I've a problem with my product import extension that is returned the "classic" violation integrity error:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '25061-1' for key 'UNQ_CATALOGINVENTORY_STOCK_ITEM_PRODUCT_ID_STOCK_ID'

I'm using Magento CE

There are multiple topics about that issue on stackoverflow, magento forums with different technical approaches like below:

  • Someone recommends to download and use the dbtools repairs provided by Magento, **.

  • Some are providing db tables list to truncate** "with no major risk".

  • An another proposal is to identify with precision the data and remove it from the related db table.

What is the pragmatic way to identify and fix that issue?

  • Check your import, be sure to load the stock item, before writing a new one May 22, 2014 at 14:32
  • 1
    the problem is, you try to create a stock item with the product_id and stock_id for a product/stock which already exists. This means you have either to delete the old one before or load it, update it and then write it back May 22, 2014 at 15:11
  • 1
    check wether you have a stock item with a reference on this product. If this is the case, your foreign key constraints seem to be broken May 22, 2014 at 15:39
  • 1
    then I guess your import creates the stock item twice? Be creative. Golden rule: Mysql is never wrong. Or to quote spock "First try all the logical solutions. If they are all disproved, continue with the illogical" Whatever happens, magento tries to create a second stockitem, therefore there must be a first. and if everything happens in an transaction, you can't see it on the outside May 22, 2014 at 16:01
  • 2
    Thank you for your help. I'll keep in mind your Golden rule ;)
    – DarkCowboy
    May 23, 2014 at 6:47

2 Answers 2


The error has been found on our import/update script (product->save() was called twice).

Important: no database "cleaning" was required.

I'm happy to have not choose to follow some advice on the web that propose to do that kind of operations (@fabian, you're golden rule was successfully followed).


This can happen if cataloginventory_stock_item table has some orphan rows whos parent records doesn't exist in catalog_product_entity

To find out the orphan rows run below query

SELECT * FROM cataloginventory_stock_item where product_id NOT IN (SELECT entity_id FROM catalog_product_entity)

Once you are done reviewing the data and convinced that you don't need it, just run below query

DELETE FROM cataloginventory_stock_item where product_id NOT IN (SELECT entity_id FROM catalog_product_entity)

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.