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

Deadly 提交于 2019-11-27 04:31:19

问题


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?


回答1:


the message means you are doing another insert with the same combination of columns that are part of the IDX_STOCK_PRODUCT, which seams to be defined as UNIQUE. If it is so, it doesn't allow to enter same combination (it seems like it consists of two fields) twice.

If you are inserting records, make sure you are picking brand new record id or that the combination of record id and the other column is unique.

Without detailed table structure and your code, we can hardly guess whats going wrong.




回答2:


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()




回答3:


You might have forgotten to auto increment the id field.




回答4:


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




回答5:


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




回答6:


Try to change the FK to INDEX instead of UNIQUE.




回答7:


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



来源:https://stackoverflow.com/questions/10044062/sqlstate23000-integrity-constraint-violation-1062-duplicate-entry-1922-1-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!