10

For a while I get the following when I go to catalog->products

Item (Magento\Catalog\Model\Product\Interceptor) with the same ID "21" already exists

That is a product existing but it's not duplicated. Also, there are no duplicated rewritten URLs or anything.

When I hard deleted the product entity from the DB I could get the table to load but still, I get all kinds of popups with errors.

There is no logging in exception.log debug.log or anything to pull in more info

1

13 Answers 13

18

Then you can use the below module to resolve the above error.

  1. Create new folders

RSCoder/DuplicateEntry in app/code Magento folder.

  1. registration.php
<?php
 
\Magento\Framework\Component\ComponentRegistrar::register(
 \Magento\Framework\Component\ComponentRegistrar::MODULE,
 'RSCoder_DuplicateEntry',
 __DIR__
);
  1. etc/module.xml
<?xml version="1.0"?>
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
 <module name="RSCoder_DuplicateEntry" setup_version="1.0.1"></module>
</config>
  1. etc/di.xml
<?xml version="1.0"?>
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <type name="Magento\Eav\Model\Entity\Collection\AbstractCollection">
    <plugin name="find_duplicate_entry" type="RSCoder\DuplicateEntry\plugin\Collection" sortOrder="20"/>
 </type>
</config>
  1. plugin/Collection.php
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 */
namespace RSCoder\DuplicateEntry\plugin;
use Magento\Framework\Data\Collection\EntityFactoryInterface;
use Magento\Framework\Option\ArrayInterface;
 
class Collection
{
    /**
     * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $subject
     * @param \Closure $process
     * @param \Magento\Framework\DataObject $dataObject
     * @return $this
     */
    public function aroundAddItem(\Magento\Eav\Model\Entity\Collection\AbstractCollection $subject, \Closure $process, \Magento\Framework\DataObject $dataObject)
    {
        try{
            return $process($dataObject);
        }catch ( \Exception $e){
            return $this;
        }
    }
}

It's Work for me.

Reference Link

5
  • this works for me!
    – hkguile
    Jan 21, 2020 at 5:46
  • Worked like charm ! Feb 6, 2020 at 10:58
  • Yes same here it worked! Using Magento 2.2.7 Thanks Apr 3, 2020 at 8:46
  • @Rahul Shukla Can this help remove the duplicate entry in the database? Jan 6 at 13:32
  • this is working but it removes the both origin value and duplicate value... I wanted to keep original value. eg. according the question it removes the 21 value from the collection . I want keep 1 value with 21 id. How can I do that ? Mar 30 at 3:30
5

As Kandy mentioned, it is a known bug and it is fixed on develop branch.

To quickly fix it you can delete all rows that have duplicated products ids in the cataloginventory_stock_item table.

4
  • I'm having this problem currently in Magento 2.1.2 and all the ID's in cataloginventory_stock_item are unique. Nov 30, 2016 at 12:42
  • @GielBerkers yeah, i have experienced this error a lot and sometimes it is not because of that table, but some other table Nov 30, 2016 at 13:16
  • I notice now I have a minor difference that is not so minor. It's not the Product but the Category: Exception #0 (Exception): Item (Magento\Catalog\Model\Category\Interceptor) with the same ID "191" already exists. Any thoughts on this matter? Nov 30, 2016 at 13:27
  • @GielBerkers no, never had it on category.. Nov 30, 2016 at 13:37
2

I have faced the same issue in Magento EE 2.3.1, Later we find that the issue was happening as there were duplicate entries in the table shared_catalog_product_item.

This is the query that we have logged

SELECT 
  `e`.*, 
  `cat_index`.`position` AS `cat_index_position`, 
  `perm`.`grant_catalog_category_view`, 
  `perm`.`grant_catalog_product_price`, 
  `perm`.`grant_checkout_items`, 
  `stock_status_index`.`is_salable`, 
  `shared_product`.`customer_group_id` 
FROM 
  `catalog_product_entity` AS `e` 
  INNER JOIN `catalog_category_product_index_store10` AS `cat_index` ON cat_index.product_id = e.entity_id 
  AND cat_index.store_id = 10 
  AND cat_index.category_id = 492 
  LEFT JOIN `magento_catalogpermissions_index` AS `perm` ON perm.customer_group_id = 0 
  AND perm.category_id = cat_index.category_id 
  AND perm.website_id = '10' 
  INNER JOIN `catalog_product_entity` AS `product` ON product.entity_id = e.entity_id 
  AND (
    product.created_in <= '1619042340' 
    AND product.updated_in > '1619042340'
  ) 
  LEFT JOIN `inventory_stock_15` AS `stock_status_index` ON product.sku = stock_status_index.sku 
  INNER JOIN `shared_catalog_product_item` AS `shared_product` ON (shared_product.sku = e.sku) 
  AND (
    shared_product.customer_group_id = '0'
  ) 
WHERE 
  (
    (
      perm.grant_catalog_category_view != -2 
      OR perm.grant_catalog_category_view IS NULL
    )
  ) 
  AND (e.created_in <= '1619042340') 
  AND (e.updated_in > '1619042340') 
ORDER BY 
  `cat_index`.`position` ASC, 
  `e`.`entity_id` DESC 
LIMIT 
  32

If you face this issue there should be some duplicate entry in any of the table from the above query.

Hope this helps

2

The following query helps you to find out the duplicate entry.

select product_id, website_id from cataloginventory_stock_item group by product_id having count(product_id) > 0;

Thanks, Sam

1

This problem exists due to the difference between products "website_id" value and default value in Magento 2

Table: cataloginventory_stock_item so first

  1. Check the default value for your site it will be at table cataloginventory_stock

  2. Then compare it to the existing products at Table: cataloginventory_stock_item in my case it was 0 while products website_id were 1 so to solve this run the following SQL UPDATE cataloginventory_stock_item SET website_id=0

0

Can happen that there are duplicated rows in the table catalog_stock_item where they only differ of website_id value

to get the duplicated rows you can run this SQL query and delete the ones with the wrong website_id

SELECT *
FROM cataloginventory_stock_item
where product_id in (SELECT product_id 
FROM cataloginventory_stock_item
GROUP BY product_id
HAVING ( COUNT(product_id) > 1 ))
0

If you have just one single store use this sql qwerry

SELECT * FROM `cataloginventory_stock_item` WHERE `website_id` = 0

and delete all search results. Backup your database first for me that works good luck

0

I have the same error when I create new products collection Exception #0 (Exception): Item (Magento\Catalog\Model\Product\Interceptor) with the same ID "1" already exists.

$collection = $this->productsCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->joinUrlRewrite();
    $collection->addOptionsToResult();
    $collection->addMediaGalleryData();

and it disappeared when I replaced this method -

$collection->joinUrlRewrite();

with

$collection->addUrlRewrite();

so check your code. Maybe you have the same problem.

0

I experienced this and found that it was caused by multiple products having the same SKU. Think this happened due to a bad product import from an external provider.

To find out if you have duplicated sku's run this:

SELECT * FROM catalog_product_entity WHERE sku IN
  (SELECT sku FROM catalog_product_entity GROUP BY sku HAVING COUNT(*) > 1);

Edit: The solution above fixed the category pages, but I also discovered its happening on some product pages. I managed to fix them manually by going to the grouped product in the admin (the ID that was throwing the error), looking at the simple products it was grouped with, and saving those simple products. Magento is obviously doing something to the database tables to clear some incorrect data when you save a product, but I still don't know exactly what.

0

I also got a similar error after migration from m1.9.4 to m2.3.5. In M1 we had a multiwarehouse module, so the product could have some qty from different warehouses. After migration, I found duplicates in cataloginventory_stock_item table. For fixing this bug I ran the following query:

DELETE FROM `cataloginventory_stock_item`
    WHERE item_id NOT IN
    (
        SELECT MIN(item_id) AS MaxRecordID
        FROM `cataloginventory_stock_item` 
        GROUP BY product_id
    );

You can also check all duplicates in your table:

SELECT product_id, COUNT(*) AS c 
    FROM `cataloginventory_stock_item` 
    GROUP BY product_id HAVING c > 1;
0

I had this issue and it turns out for me it was nothing to do with catalog_inventory_stock_item at all, my issue actually lay in the catalog_product_entity_varchar table. What caused this for me is that the product I was saving programmatically was saving the data under a different store id. Try doing

SELECT * FROM catalog_product_entity_varchar WHERE entity_id = {entity id that is in your error};

look through the resulting rows, if any of them have a store_id other than 0 (or whatever your base store id is) then make a backup of the table then try removing those and refresh the cache then the page. If you have the issue I had, it'll go away.

0

I discuss it on google And I follow all instruction but still, this problem is not solving. Here is the link to my discussion, You can check it here.

https://magento.stackexchange.com/questions/173358/item-magento-catalog-model-category-interceptor-with-the-same-id-252-already

1
  • did you get any solution ?
    – Camit1dk
    Nov 1, 2017 at 9:10
0

Run SQL

SELECT product_id ,COUNT(*) FROM cataloginventory_stock_status GROUP BY product_id, product_id HAVING COUNT(*) > 1

After run above sql you can get all duplicate ids then run the below SQL query.

delete from cataloginventory_stock_status where  product_id = {AddDuplicateId} and website_id != 0 ;

update cataloginventory_stock_status set stock_status = 1 where  product_id = {AddDuplicateId} and qty >= 1 ;

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