Magento Error – Integrity Constraint Violation: 1052 Column ‘increment_id’ in Where Clause is Ambiguous

adminerrorgridmagento-1.8

I am trying to create a customer grid module that accesses different table columns from sales_order information such as billing, payment method etc..

I have the grids show the correct data but when I search the order number field I get

"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'increment_id'

Can anyone tell me how I can solve this so I can search any field from the grid in order to sort the data please?

Grid.php

<?php

class Custom_Module_Block_Adminhtml_Order_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

    public function __construct()
    {
        parent::__construct();
        $this->setId('sales_order_grid');
        $this->setUseAjax(false);
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
    }

    protected function _getCollectionClass()
    {
        return 'sales/order_grid_collection';
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());
        $collection->getSelect()->joinLeft('sales_flat_order_payment',
        'sales_flat_order_payment.parent_id = main_table.entity_id',array('cc_type',
        'cc_number_enc', 'cc_type', 'last_trans_id'));

        $collection->getSelect()->joinLeft('sales_payment_transaction',
            'sales_payment_transaction.transaction_id = main_table.entity_id',array('payment_id'));

        $collection->getSelect()->joinLeft(
                'customer_entity',
                'main_table.customer_id = customer_entity.entity_id', array('customer_name' => 'email'));

        $collection->getSelect()->joinLeft(
                'ops_alias',
                'sales_flat_order_payment.parent_id = main_table.entity_id', array('alias','brand', 'payment_method'));

        $this->setCollection($collection);
        return parent::_prepareCollection();
    }


    protected function _prepareColumns()
    {
        $this->addColumn('real_order_id', array(
            'header'=> Mage::helper('sales')->__('Order #'),
            'width' => '80px',
            'type'  => 'text',
            'index' => 'increment_id',

        ));

        $this->addColumn("created_at", array(
            "header" => Mage::helper("sales")->__("Order Date"),
            "index" => "created_at",
        ));

        $this->addColumn("billing_name", array(
            "header" => Mage::helper("sales")->__("Billing Name"),
            "index" => "billing_name",
        ));

        $this->addColumn("customer_name", array(
            "header" => Mage::helper("sales")->__("Customer Email"),
            "index" => "customer_name",
        ));

        $this->addColumn("payment_method", array(
            "header" => Mage::helper("sales")->__("Payment Method"),
            "index" => "payment_method",
        ));

        $this->addColumn('payment_id', array(
            'header'        => Mage::helper('sales')->__('Payment ID'),
            'align'         => 'right',
            'index'         => 'payment_id',
        ));

        $this->addColumn('brand', array(
            'header'    => Mage::helper('ops')->__('Credit Card Type'),
            'index'     => 'brand',
        ));

        $this->addColumn('alias', array(
            'header'        => Mage::helper('ops')->__('Alias'),
            'align'         => 'right',
            'index'         => 'alias',
        ));

        $this->addColumn('cc_number_enc', array(
            'header'    => Mage::helper('sales')->__('CC Last4'),
            'index'     => 'cc_number_enc',
        ));

        $this->addColumn('grand_total', array(
            'header' => Mage::helper('sales')->__('Order Total'),
            'index' => 'grand_total',
            'type'  => 'currency',
            'currency' => 'order_currency_code',
        ));

        $this->addColumn('status', array(
            'header' => Mage::helper('sales')->__('Status'),
            'index' => 'status',
            'type'  => 'options',
            'width' => '70px',
            'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
        ));

        if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
            $this->addColumn('action',
                array(
                    'header'    => Mage::helper('sales')->__('Action'),
                    'width'     => '50px',
                    'type'      => 'action',
                    'getter'     => 'getId',
                    'actions'   => array(
                        array(
                            'caption' => Mage::helper('sales')->__('View'),
                            'url'     => array('base'=>'adminhtml/sales_order/view'),
                            'field'   => 'order_id'
                        )
                    ),
                    'filter'    => false,
                    'sortable'  => false,
                    'index'     => 'stores',
                    'is_system' => true,
                ));
        }

        $this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV'));
        $this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel'));

        return parent::_prepareColumns();
    }

    public function getRowUrl($row)
    {
        //return $this->getUrl("*/*/edit", array("id" => $row->getId()));
    }

} 

Best Answer

Solved it!

The increment_id column need to have the additional

'filter_index'=>'main_table.increment_id',

So the Grid column now looks like this:

$this->addColumn('order_id', array(
    'header' => Mage::helper('sales')->__('Order Id'),
    'align' =>'left',
    'index' => 'increment_id',
    'filter_index'=>'main_table.increment_id',
));