Question

What I should do if Product Attributes index gives an error like this:

Next exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '12692-214-1-11745' for key 'PRIMARY'' in /home/uitlaat/domains/uitlaatcity.nl/public_html/lib/Zend/Db/Statement/Pdo.php:234
Stack trace:
#0 /home/uitlaat/domains/uitlaatcity.nl/public_html/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#1 /home/uitlaat/domains/uitlaatcity.nl/public_html/lib/Zend/Db/Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#2 /home/uitlaat/domains/uitlaatcity.nl/public_html/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#3 /home/uitlaat/domains/uitlaatcity.nl/public_html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `ca...', Array)
#4 /home/uitlaat/domains/uitlaatcity.nl/public_html/lib/Varien/Db/Adapter/Pdo/Mysql.php(419): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `ca...', Array)
#5 /home/uitlaat/domains/uitlaatcity.nl/public_html/lib/Varien/Db/Adapter/Pdo/Mysql.php(1974): Varien_Db_Adapter_Pdo_Mysql->query('INSERT INTO `ca...', Array)
#6 /home/uitlaat/domains/uitlaatcity.nl/public_html/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Eav/Source.php(276): Varien_Db_Adapter_Pdo_Mysql->insertArray('catalog_product...', Array, Array)
#7 /home/uitlaat/domains/uitlaatcity.nl/public_html/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Eav/Source.php(257): Mage_Catalog_Model_Resource_Product_Indexer_Eav_Source->_saveIndexData(Array)
#8 /home/uitlaat/domains/uitlaatcity.nl/public_html/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Eav/Source.php(84): Mage_Catalog_Model_Resource_Product_Indexer_Eav_Source->_prepareMultiselectIndex(NULL, NULL)
#9 /home/uitlaat/domains/uitlaatcity.nl/public_html/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Eav/Abstract.php(50): Mage_Catalog_Model_Resource_Product_Indexer_Eav_Source->_prepareIndex()
#10 /home/uitlaat/domains/uitlaatcity.nl/public_html/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Eav.php(185): Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract->reindexAll()
#11 /home/uitlaat/domains/uitlaatcity.nl/public_html/app/code/core/Mage/Index/Model/Indexer/Abstract.php(143): Mage_Catalog_Model_Resource_Product_Indexer_Eav->reindexAll()
#12 /home/uitlaat/domains/uitlaatcity.nl/public_html/app/code/core/Mage/Index/Model/Process.php(209): Mage_Index_Model_Indexer_Abstract->reindexAll()
#13 /home/uitlaat/domains/uitlaatcity.nl/public_html/app/code/core/Mage/Index/Model/Process.php(255): Mage_Index_Model_Process->reindexAll()
#14 /home/uitlaat/domains/uitlaatcity.nl/public_html/shell/indexer.php(158): Mage_Index_Model_Process->reindexEverything()
#15 /home/uitlaat/domains/uitlaatcity.nl/public_html/shell/indexer.php(198): Mage_Shell_Compiler->run()
#16 {main}

Who has some solutions, maybe It helps me if I truncate some tables.

Was it helpful?

Solution

I debugged step-by-step and found exact reason of trace given above. I have custom script to importing products to Magento, and while creating new product, script attaches multiselect attribute values to the product. Some of the products have one option id two or more times and during indexation this will be cause of some bug like "Integrity constraint violation". If I describe this by code:

$_product->setSomeMultiselectAttribute(array(option_id_1, option_id_2, option_id_1));
$_product->save();

After finished importing, I do indexation, and exception given above will be appear. Thats way I fixed my bug:

$_product->setSomeMultiselectAttribute(array_unique(array(option_id_1, option_id_2, option_id_1)));

Maybe this will help somebody in future. And exception has gone.

OTHER TIPS

In most cases this is related to the flat_catalog tables having an issue with a key being inserted that's already in the database i.e. a primary key constraint. Truncating them is indeed a good option, the keys will be cleared and you can reindex them afterward.

In mysql: (keep in mind that your flat tables may have different names)

TRUNCATE TABLE `catalog_category_flat_store_1`;

Then reindex from adminpanel or via terminal execute the reindexing shell-script from Magento root directory as follows:

php shell/indexer.php --reindex catalog_product_flat 
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top