In Magento, how do I populate a new column in sales_order_grid with data from sales_flat_order?

In Magento, how do I populate a new column in sales_order_grid with data from sales_flat_order?

August 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
In Magento, how do I populate a new column in sales_order_grid with data from sales_flat_order?

Populating New Columns in Magento Sales Order Grid from Existing Data

In Magento, adding new columns to the sales order grid is a common task for extending the default functionality of the platform. While Ivan’s tutorial provides a good starting point, it leaves out an important step—the migration of existing data into the newly created column.

The Issue: Missing Old Data in New Column

You’ve successfully added the ‘shipping_description’ column to the sales_order_grid table using your SQL install script. However, when you check the grid, you find that the new column is empty for existing orders—only new orders are populating this field with data.

The problem lies in Magento’s way of handling the sales_flat_order_grid and sales_flat_order tables. The grid table is a summary of order data optimized for fast filtering and searching, while the main order table contains all the detailed information about each order.

Your SQL script correctly adds the new column to both tables, but Magento’s update process doesn’t automatically migrate existing data from sales_flat_order into the new shipping_description field in sales_flat_order_grid.

The Solution: Cross-Update from Select

To resolve this issue, you need to manually copy the existing ‘shipping_description’ data from the sales_flat_order table to the sales_flat_order_grid table. Here’s how you can do it:

  1. Create a new file named InstallData.php in the root directory of your module.

  2. Add the following code to this file:

<?php

namespace YourVendor\YourModule;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SetupInterface;

class InstallData implements InstallDataInterface
{
    private $resourceConnection;

    public function __construct(ResourceConnection $resource)
    {
        $this->resourceConnection = $resource;
    }

    public function install(SetupInterface $setup, ModuleContextInterface $context)
    {
        $connection = $this->resourceConnection->getConnection();

        // Add column to grid table
        $connection->addColumn(
            $setup->getTable('sales/order_grid'),
            'shipping_description',
            "varchar(255) not null default ''"
        );

        // Add key for field for better performance
        $connection->addKey(
            $setup->getTable('sales/order_grid'),
            'shipping_description',
            'shipping_description'
        );

        // Copy existing data from sales_flat_order to sales_order_grid
        $select = $connection->select();
        $select->join(
            array('order' => $setup->getTable('sales/order')),
            'main_table.entity_id = order.entity_id',
            array('shipping_description' => 'shipping_description')
        );

        $connection->query(
            $select->crossUpdateFromSelect(
                array('main_table' => $setup->getTable('sales/order_grid'))
            )
        );
    }
}

Replace YourVendor and YourModule with the vendor name and module name respectively.

  1. Make sure your installer script runs after the data migration scripts in app/code/core/Mage/Sales/sql/sales_setup/install-1.0.0.php. You can achieve this by setting a later sequence number in your setup file, for example:
<sequence>
    <module name="Mage_Sales" />
</sequence>
  1. Clear Magento’s cache and re-index the data.

Why This Works

The crossUpdateFromSelect method allows you to update one table with the results of a SELECT query from another table. In this case, it updates the sales_order_grid table (the “main_table”) with the ‘shipping_description’ value obtained from the joined sales_flat_order table (“order”).

Conclusion

Populating new columns in Magento’s order grid with existing data requires a manual migration step using a cross-update query. By following this approach and ensuring your installer script runs after the core sales setup, you can ensure that your new column is populated with both old and new data.

Remember to always test your changes thoroughly before deploying them to a production environment. For more detailed information on Magento’s installation process and best practices, I recommend checking out our guide at Laravel Company.

Happy coding!

Database Performance Extensions Admin

Our network