[magento] SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1922-1' for key 'IDX_STOCK_PRODUCT'

While creating product, at the last step after retrieving for a time, Magento gives following error-:

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

What I am doing is, by capturing product id, I am putting it's entry in custom table. I have connected to Magento database externally.

Surprisingly data is inserted in both Magento's base table & also in Custom table but why it is giving me that error after product saving...?

I cleared cache, browser cookies. Also remove /var/cache, /var/session. still giving error. Can anybody suggest a solution?

This question is related to magento constraints mysql-error-1062 duplicates

The answer is


your column value is already in database table it means your table column is Unique you should change your value and try again


You might have forgotten to auto increment the id field.


I just added an @ symbol and it started working. Like this: @$product->save();


Many time this error is caused when you update a product in your custom module's observer as shown below.

class [NAMESPACE]_[MODULE NAME]_Model_Observer
{
    /**
     * Flag to stop observer executing more than once
     *
     * @var static bool
     */
    static protected $_singletonFlag = false;

    public function saveProductData(Varien_Event_Observer $observer)
    {
        if (!self::$_singletonFlag) {
            self::$_singletonFlag = true;

            $product = $observer->getEvent()->getProduct();
             //do stuff to the $product object
            // $product->save();  // commenting out this line prevents the error
            $product->getResource()->save($product);
    }
} 

Hence whenever you save your product after updating some properties in your module's observer use $product->getResource()->save($product) instead of $product->save()


Try to change the FK to INDEX instead of UNIQUE.


I also encountered this problem. I found after changing the table storage engine from MyISAM to Innodb, problem solved .


Examples related to magento

Where are Magento's log files located? find . -type f -exec chmod 644 {} ; Get product id and product type in magento? There has been an error processing your request, Error log record number Class 'DOMDocument' not found Magento: Set LIMIT on collection How to remove index.php from URLs? Get skin path in Magento? SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1922-1' for key 'IDX_STOCK_PRODUCT' Service Temporarily Unavailable Magento?

Examples related to constraints

How to trap on UIViewAlertForUnsatisfiableConstraints? trying to animate a constraint in swift Remove all constraints affecting a UIView "Insert if not exists" statement in SQLite Unique Key constraints for multiple columns in Entity Framework SQL Server 2008- Get table constraints How to remove constraints from my MySQL table? Creating layout constraints programmatically Display names of all constraints for a table in Oracle SQL Add primary key to existing table

Examples related to mysql-error-1062

Error Code: 1062. Duplicate entry '1' for key 'PRIMARY' MySQL duplicate entry error even though there is no duplicate entry #1062 - Duplicate entry for key 'PRIMARY' SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1922-1' for key 'IDX_STOCK_PRODUCT' org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

Examples related to duplicates

Remove duplicates from dataframe, based on two columns A,B, keeping row with max value in another column C Remove duplicates from a dataframe in PySpark How to "select distinct" across multiple data frame columns in pandas? How to find duplicate records in PostgreSQL Drop all duplicate rows across multiple columns in Python Pandas Left Join without duplicate rows from left table Finding duplicate integers in an array and display how many times they occurred How do I use SELECT GROUP BY in DataTable.Select(Expression)? How to delete duplicate rows in SQL Server? Python copy files to a new directory and rename if file name already exists