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

后端 未结 7 608
遥遥无期
遥遥无期 2020-12-09 11:16

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

SQLSTATE[23000]: Integrity constraint violation:

相关标签:
7条回答
  • 2020-12-09 11:50

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

    0 讨论(0)
  • 2020-12-09 11:50

    Try to change the FK to INDEX instead of UNIQUE.

    0 讨论(0)
  • 2020-12-09 11:57

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

    0 讨论(0)
  • 2020-12-09 12:00

    You might have forgotten to auto increment the id field.

    0 讨论(0)
  • 2020-12-09 12:01

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

    0 讨论(0)
  • 2020-12-09 12:04

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

    0 讨论(0)
提交回复
热议问题