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

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.


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.


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/

Tags:

Magento 1.7