20

I have been getting this error when trying to filter a products collection

Item (Mage_Catalog_Model_Product) with the same id "6058" already exist and wanted to ask what could ne causing the error since there is only one (visible) product with the same ID inside of Magento.

Is there a table that needs to be cleared to remove this dupilcate?

2

4 Answers 4

36

Adding the distinct as suggested in the accepted answer does fix the problem but it has performance issues. The database might create temporary tables on disk when executing a query with distinct and this will slow down your request. You can instead add a group condition to the collection to remove duplicates.

Take a look at this post. What they did (and I have done it also) is group by the entity id. This should perform better.

//adding filters to the collection..

$collection->getSelect()
           ->group('e.entity_id');
6
  • can you please tell where we need to add this code Commented Jul 22, 2016 at 12:53
  • @BabyinMagento wherever it is you're using your collection in your custom code. This error occurs when creating custom modules that fetch collections from the database. This means it could show up if you have installed third party modules recently. In that case, it is up to the developer of said module to find the location of the error.
    – Vic
    Commented Jul 28, 2016 at 0:34
  • The distinct() solution and this will remove any additional records from the query, which means you might lose data without noticing. So this might not be the best solution, as in my case. I'm now trying to find a way to get both the records from my joined table into the collection so that I can show both records on the same row in grid.
    – Jacques
    Commented Jan 23, 2017 at 15:20
  • Little note: its importend to not chain this to the collection, but furst put your colelction to a variable and than do $collection->getSelect() ->group('e.entity_id');
    – Rickert
    Commented Mar 2, 2017 at 11:13
  • where can i update this code?
    – zus
    Commented Jan 10, 2018 at 9:49
10

Normally, this is a bug in the data or in the collection implementation.

Here's a solution to a broader issue. This works on arbitrary collection, not only for Catalog_Model_Product.

Step 1. Modify the core file lib/Varien/Data/Collection.php, function addItem(), but unlike this answer suggests, don't hide the error.

Instead, add extra error information to the exception thrown:

        if (isset($this->_items[$itemId])) {
            throw new Exception('Item ('.get_class($item).
                ') with the same id "'.$item->getId().'" already exist' .
                '. SQL that caused this: ' . $this->getSelect());
        }

Step 2. Take the offending query from your error report and run it by hand. See what records duplicate the collection key. Add order by <key field> as needed.

Dissect the query removing the participating tables one-by-one, and see which record caused the duplication.

I believe this patch should be in the core.

1
  • This should be the correct answer. Other answers had me thinking it was a bug in how Magento was building queries, but I felt uncomfortable with that all the solutions until I saw this. Dumped my select and saw that the bug was in how I built the query.
    – scorgn
    Commented Jan 21, 2021 at 17:53
3

Your issue is that you have a collection (likely with a join or union) that is resulting in the same product being loaded into the collection twice.

You can alter the collection being loaded by adding a distinct method to the select object.

See http://framework.zend.com/manual/1.12/en/zend.db.select.html

$db->select()
         ->distinct()

But this comes with inherent problems. Using distinct will cause temporary tables to be created on disk, not in memory, which comes with performance penalties.

0

In my case

->getSelect()->group('e.entity_id');

not working i use:

->getSelect()->group('main_table.entity_id');

Mat be hejpful for some one as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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