问题
Magento 1.7 cannot reindex product flat data... I get the following error when trying to reindex my database.
Product Flat Data index process unknown error:
exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`d014505f`.<result 2 when explaining filename '#sql-1f6c_39a11d'>, CONSTRAINT `FK_CAT_PRD_FLAT_1_ENTT_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`entity_id`) REFERENCES `catalog_product_entity` (`e)' in /www/htdocs/w00f5624/lib/Zend/Db/Statement/Pdo.php:228
Stack trace:#0 /www/htdocs/w00f5624/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement- >execute(Array)
#1 /www/htdocs/w00f5624/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#2 /www/htdocs/w00f5624/lib/Zend/Db/Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#3 /www/htdocs/w00f5624/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#4 /www/htdocs/w00f5624/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('ALTER TABLE `ca...', Array)
#5 /www/htdocs/w00f5624/lib/Varien/Db/Adapter/Pdo/Mysql.php(419): Zend_Db_Adapter_Pdo_Abstract->query('ALTER TABLE `ca...', Array)
#6 /www/htdocs/w00f5624/lib/Varien/Db/Adapter/Pdo/Mysql.php(340): Varien_Db_Adapter_Pdo_Mysql->query('ALTER TABLE `ca...')
#7 /www/htdocs/w00f5624/lib/Varien/Db/Adapter/Pdo/Mysql.php(2569): Varien_Db_Adapter_Pdo_Mysql->raw_query('ALTER TABLE `ca...')
#8 /www/htdocs/w00f5624/app/code/core/Mage/Catalog/Model/Resource/Product/Flat/Indexer.php(816): Varien_Db_Adapter_Pdo_Mysql->addForeignKey('FK_CAT_PRD_FLAT...', 'catalog_product...', 'entity_id', 'catalog_product...', 'entity_id', 'CASCADE', 'CASCADE')
#9 /www/htdocs/w00f5624/app/code/core/Mage/Catalog/Model/Resource/Product/Flat/Indexer.php(1390): Mage_Catalog_Model_Resource_Product_Flat_Indexer->prepareFlatTable(1)
#10 /www/htdocs/w00f5624/app/code/core/Mage/Catalog/Model/Product/Flat/Indexer.php(296): Mage_Catalog_Model_Resource_Product_Flat_Indexer->reindexAll()
#11 /www/htdocs/w00f5624/app/code/core/Mage/Catalog/Model/Product/Indexer/Flat.php(336): Mage_Catalog_Model_Product_Flat_Indexer->reindexAll()
#12 /www/htdocs/w00f5624/app/code/core/Mage/Index/Model/Process.php(209): Mage_Catalog_Model_Product_Indexer_Flat->reindexAll()
#13 /www/htdocs/w00f5624/app/code/core/Mage/Index/Model/Process.php(255): Mage_Index_Model_Process->reindexAll()
#14 /www/htdocs/w00f5624/shell/indexer.php(158): Mage_Index_Model_Process->reindexEverything()
#15 /www/htdocs/w00f5624/shell/indexer.php(198): Mage_Shell_Compiler->run()
#16 {main}
回答1:
magento programatically re index
ID Code
1 catalog_product_attribute
2 catalog_product_price
3 catalog_url
4 catalog_product_flat
5 catalog_category_flat
6 catalog_category_product
7 catalogsearch_stock
8 cataloginventory_stock
9 tag_summary
for ($i = 1; $i <= 9; $i++) {
$process = Mage::getModel('index/process')->load($i);
$process->reindexAll();
}
回答2:
It seems Magento did not clean the table when you have deleted some informations; so you need to clean it manually, using this SQL query:
TRUNCATE TABLE ´catalog_product_flat_1´;
Then, run reindex process.
It's okay to empty that table; since Magento uses EAV tables to rebiuld (reindex) it again.
回答3:
I experienced the same issue today. To fix this, just locate the corrupted products by running
SELECT cpf.entity_id FROM catalog_product_flat_1 AS cpf LEFT JOIN catalog_product_entity AS cpe ON cpf.entity_id = cpe.entity_id WHERE ISNULL(cpe.entity_id);
You'll get a result like
+-----------+
| entity_id |
+-----------+
| 14029 |
| 14111 |
+-----------+
2 rows in set (0.01 sec)
Now you can just delete these products by running
DELETE FROM catalog_product_flat_1 where entity_id IN (14029,14111);
Note: You might need to change the "catalog_product_flat_1" table - the error message tells you which table contains the corrupted products.
回答4:
I've got almost the same error: (something like that) SQLSTATE[HY000]: General error: 1005 Can't create table 'databasename.#sql-4ebf-e07' (errno: 121)
Then I've researched that I have the same foreign key 'FK_CAT_PRD_FLAT_1_ENTT_ID_CAT_PRD_ENTT_ENTT_ID'
Even if I truncate a table like this:
SET FOREIGN_KEY_CHECKS=0;
TRUNCATE TABLE catalog_product_flat_1;
SET FOREIGN_KEY_CHECKS=1;
didn't solve my problem.
Even if I drop all tables in DB, I couldn't drop only 3 of them at all: catalog_product_entity, and couple eav_ tables (I don't remember)
Only one way had helped me:
- Make backup of current DB (do this before any changes, even if you've got message to reindex)
- Drop DB (not all tables, but directly DB)
- Create DB (check that you still have privileges)
- Restore DB from backup and check Admin panel.
回答5:
SET FOREIGN_KEY_CHECKS=0;
TRUNCATE TABLE catalog_product_flat_1;
TRUNCATE TABLE catalog_product_flat_2;
SET FOREIGN_KEY_CHECKS=1;
Worked for me.
Afterwards I could reindex from CLI.
来源:https://stackoverflow.com/questions/15574547/magento-1-7-cannot-reindex-product-flat-data