17

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?

Manashvi Birla
  • 2,837
  • 3
  • 14
  • 28
Prat
  • 519
  • 5
  • 16
  • 33
  • 1
    This error means you're using the same primary key value twice in the same table. There is already a product with the ID of `1922-1` in `IDX_STOCK_PRODUCT`, and a query is trying to save a new record with that ID again. – leemeichin Apr 06 '12 at 13:31
  • 1
    Whats the solution then? – Prat Apr 06 '12 at 13:35
  • 1
    Don't save data with a duplicate ID, basically. Without any code samples relevant to this error, that's the best solution you're going to get. – leemeichin Apr 06 '12 at 13:42

7 Answers7

16

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.

Radek
  • 519
  • 5
  • 16
  • You are right Radek, I am fetching SKU & entity_id in my custom table. In my custom table I have just 4 columns-: No.,entity_id,SKU & timestamp....What constraint I should put in that table? – Prat Apr 06 '12 at 13:43
3

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

Tahir Yasin
  • 11,489
  • 5
  • 42
  • 59
1

You might have forgotten to auto increment the id field.

Rhys
  • 21
  • 1
1

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

Mario7
  • 83
  • 1
  • 12
0

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

Adeel Ahmed Baloch
  • 672
  • 2
  • 7
  • 15
  • Please edit your question for clarity and content 1) Break down your answer to sentences 2) IDX_STOCK_PRODUCT is probably a compound key, not a column. 3) Once you update your answer add some additional useful information to it so it's not just a duplicate or subset of other answers. – Michal Sep 17 '18 at 08:13
-1

Try to change the FK to INDEX instead of UNIQUE.

gdm
  • 7,647
  • 3
  • 41
  • 71
-5

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

Freejoy
  • 283
  • 4
  • 18
  • 2
    The usage of `@` is a bad practice that must be avoided at all costs! – Victor Schröder Aug 09 '16 at 10:45
  • 2
    As Victor says @ should be avoided as it suppresses error messages. Details can be found about [PHP Error Control here](http://php.net/manual/en/language.operators.errorcontrol.php) – PanPipes Aug 25 '16 at 09:47