12

After creating a shipment in Magento's admin area, the following error appears in the var/report folder (after a default Magento crash page) when viewing the admin order grid page:

Item (Mage_Sales_Model_Order) with the same id "1234" already exist

I can get rid of this error by adding a DISTINCT clause to the varien object selector in app/code/core/Mage/Eav/Model/Entity/Collection/Abstact.php, line 662. However, I really don't want to do that for several reasons (deathly slow queries, editing core files, etc).

What is the best way to solve this while maintaining the data integrity. Should I delete the order? I would appreciate any constructive input, especially about what would cause this conflict in the first place. Thanks.

-- edit --

Here's the full stack trace for clarity/to point out my stupidity:

a:5:{i:0;s:67:"Item (Mage_Sales_Model_Order) with the same id "1234" already exist";i:1;s:4829:"#0 lib/Varien/Data/Collection/Db.php(576): Varien_Data_Collection->addItem(Object(Mage_Sales_Model_Order))
#1 app/code/core/Mage/Adminhtml/Block/Widget/Grid.php(533): Varien_Data_Collection_Db->load()
#2 app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php(61): Mage_Adminhtml_Block_Widget_Grid->_prepareCollection()
#3 app/code/core/Mage/Adminhtml/Block/Widget/Grid.php(626): Mage_Adminhtml_Block_Sales_Order_Grid->_prepareCollection()
#4 app/code/core/Mage/Adminhtml/Block/Widget/Grid.php(632): Mage_Adminhtml_Block_Widget_Grid->_prepareGrid()
#5 app/code/core/Mage/Core/Block/Abstract.php(862): Mage_Adminhtml_Block_Widget_Grid->_beforeToHtml()
#6 app/code/core/Mage/Core/Block/Abstract.php(582): Mage_Core_Block_Abstract->toHtml()
#7 app/code/core/Mage/Core/Block/Abstract.php(526): Mage_Core_Block_Abstract->_getChildHtml('grid', true)
#8 app/code/core/Mage/Adminhtml/Block/Widget/Grid/Container.php(77): Mage_Core_Block_Abstract->getChildHtml('grid')
#9 app/design/adminhtml/default/default/template/widget/grid/container.phtml(36): Mage_Adminhtml_Block_Widget_Grid_Container->getGridHtml()
#10 app/code/core/Mage/Core/Block/Template.php(241): include('/var/www/html/m...')
#11 app/code/core/Mage/Core/Block/Template.php(272): Mage_Core_Block_Template->fetchView('adminhtml/defau...')
#12 app/code/core/Mage/Core/Block/Template.php(286): Mage_Core_Block_Template->renderView()
#13 app/code/core/Mage/Adminhtml/Block/Template.php(81): Mage_Core_Block_Template->_toHtml()
#14 app/code/core/Mage/Adminhtml/Block/Widget/Container.php(308): Mage_Adminhtml_Block_Template->_toHtml()
#15 app/code/core/Mage/Core/Block/Abstract.php(863): Mage_Adminhtml_Block_Widget_Container->_toHtml()
#16 app/code/core/Mage/Core/Block/Text/List.php(43): Mage_Core_Block_Abstract->toHtml()
#17 app/code/core/Mage/Core/Block/Abstract.php(863): Mage_Core_Block_Text_List->_toHtml()
#18 app/code/core/Mage/Core/Block/Abstract.php(582): Mage_Core_Block_Abstract->toHtml()
#19 app/code/core/Mage/Core/Block/Abstract.php(526): Mage_Core_Block_Abstract->_getChildHtml('content', true)
#20 app/design/adminhtml/default/default/template/page.phtml(74): Mage_Core_Block_Abstract->getChildHtml('content')
#21 app/code/core/Mage/Core/Block/Template.php(241): include('/var/www/html/m...')
#22 app/code/core/Mage/Core/Block/Template.php(272): Mage_Core_Block_Template->fetchView('adminhtml/defau...')
#23 app/code/core/Mage/Core/Block/Template.php(286): Mage_Core_Block_Template->renderView()
#24 app/code/core/Mage/Adminhtml/Block/Template.php(81): Mage_Core_Block_Template->_toHtml()
#25 app/code/core/Mage/Core/Block/Abstract.php(863): Mage_Adminhtml_Block_Template->_toHtml()
#26 app/code/core/Mage/Core/Model/Layout.php(555): Mage_Core_Block_Abstract->toHtml()
#27 app/code/core/Mage/Core/Controller/Varien/Action.php(390): Mage_Core_Model_Layout->getOutput()
#28 app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php(95): Mage_Core_Controller_Varien_Action->renderLayout()
#29 app/code/core/Mage/Core/Controller/Varien/Action.php(419): Mage_Adminhtml_Sales_OrderController->indexAction()
#30 app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('index')
#31 app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#32 app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#33 app/Mage.php(683): Mage_Core_Model_App->run(Array)
#34 index.php(71): Mage::run('base', 'website')
#35 {main}";s:3:"url";s:72:"/index.php/admin/sales_order/index/key/0b7375aca52608483edc0cf879bd4361/";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:5:"admin";}
  • 8
    FWIW I hate this dumb error and have spent more time than I care to admit debugging it. – philwinkle Jan 7 '14 at 23:02
  • Can you include your collection query or php code? – Sukeshini Jan 8 '14 at 4:45
10

You added something to the collection, which adds duplicates to the query result.

What magento does with the query result is, to generate object from each row and then add these items to the collection. If the item already exists, this error is thrown.

Solution: Whatever join you added, check that your result is distinct.

If you added more fields beside the ones from sales_flat_order, these fields might differ, and therefore they are note filtered with the DISTINCT.

6

I'm adding an answer since I couldn't yet add a comment. I agree with @fabian - however I discourage the use of DISTINCT and GROUP BY, usually you can filter your join to a single distinct result if you plan correctly.

For example (I know this is a trivial example):

If you were to collect an order using sales/flat_order and you joined the customers' address, there are two records stored a billing address and delivery address. To solve this issue one can specify which type of address they want to join.

This sounds similar to your situation, except not this scenario.

3

We have also face some issue in our system. We have analyzed issue and found my Grid.php (example: /namespace/modulename/Block/Adminhtml/Sales/Order/Grid.php)

We have found _prepareCollection() function in Grid.php file.

Error coding are:

//Error coding
$collection->join(array('payment' => 'sales/order_payment'), 'main_table.entity_id = parent_id', 'method')
            ->getSelect()
            ->where("`payment`.`method` like '%paypal%'");

Solution:

//Solution coding
$collection->join(array('payment' => 'sales/order_payment'), 'main_table.entity_id = parent_id', 'method')
            ->getSelect()
            ->where("`payment`.`method` like '%paypal%'")
            ->group('method');

We have use ->group('value') in filter function. Now working fine...!!!

3

I know this is an old question and it has already been answered but I want to add my answer because I feel it could help other facing similar problems. I am trying to explain the general causes of this type of error.

To start with, Mage_Sales_Model_Order in a standard Magento installation should never give such an error. So I suspect this error is caused by a third party extension (observer, hook method, rewrite, etc) or an improper code customization that corrupted data in the first place. When talking about core components acting "weird" the distinct solution should not even exist because data should have never been corrupted in the first place! Better fix the data itself first than changing the code.

I faced a similar problem when I built a custom EAV entity type and everything worked fine until I editted one of the entities. Somehow, the editting resulted in duplicate value for one of the entity's text attributes, that is, two rows in the text value table for the same attribute of the same entity. Raw querying only the custom_entity table looked perfecly fine with no duplicates (how could it be? primary key constraint already ensured the integrity) so the problem was somewhere else.

As you might have already guessed when loading the collection, because of the joins used to retrieve the rows from database and considering there were two same text values for the same attribute of an the entity, it retrieved two rows with same entity_id value but different value_id.

As a general rule, when such an error arises you should suspect that either data in database has already been corrupted or that the joins that take place down the final query will retrieve two almost exact rows from the database, duplicating the unique identifier of the collection items.

Hope this helps.

P.S: As to why the duplicated eav text value in my case, check Marius's comment here http://inchoo.net/magento/creating-an-eav-based-models-in-magento/

0

This is my code has working well after got same problem:

    $collection = Mage::getResourceModel('sales/order_grid_collection');
    $prefix = Mage::getConfig()->getTablePrefix();  

    $collection->getSelect()->joinleft(array('order'=> $prefix.'sales_flat_order'),'order.entity_id=main_table.entity_id',array('pdeliverydate'));   

    $collection->getSelect()->joinleft(array('address'=> $prefix.'sales_flat_order_address'),'address.parent_id=main_table.entity_id',array('telephone'))->group('entity_id');

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

I have add ->group('entity_id') to solve problem.

-1

you have to try to override Mage_Sales_Model_Resource_Order_Grid_Collection::addItem which throws this exeption :

<?php
class Fixed_Order_Grid_Collection extends Mage_Sales_Model_Resource_Order_Grid_Collection{

    public function addItem(Varien_Object $item)
    {
        $itemId = $this->_getItemId($item);

        if (!is_null($itemId)) {
            if (isset($this->_items[$itemId])) {
                //throw new Exception('Item ('.get_class($item).') with the same id "'.$item->getId().'" already exist');
            }
            $this->_items[$itemId] = $item;
        } else {
            $this->_addItem($item);
        }
        return $this;
    }
}
class MyCompony_MyExtention_Block_Adminhtml_OrderGrid extends Mage_Adminhtml_Block_Widget_Grid
{
     public function __construct()
    {
        parent::__construct();
        $this->setId('sales_order_grid');
        $this->setUseAjax(true);
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
    }
    protected function _getCollectionClass()
    {
        return 'sales/order_grid_collection';
    }

    protected function _prepareCollection()
    {
        $collection = new Fixed_Order_Grid_Collection();
        $select = $collection->getSelect();
        $select->join('sales_flat_order_item AS order_item', 'order_item.order_id=main_table.entity_id','quote_item_id',NULL);
        $select->distinct();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }
...
  • 1
    This is not a solution, but simply a way to get rid of the error message. – Niels Apr 19 '17 at 12:23
  • 1
    Squashing errors is not the same as fixing them, consider adding distinct to your collection result or group the join query by a field. – DWils Sep 25 '17 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.