Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

mysql - Magento: a foreign key constraint fails after update 1.4 -> 1.7

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (my_magento/catalog_category_product_index, CONSTRAINT FK_CAT_CTGR_PRD_IDX_CTGR_ID_CAT_CTGR_ENTT_ENTT_ID FOREIGN KEY (category_id) REFERENCES catalog_category_entity (entity_id) O)

This is the error I'm getting when trying to reindex data (outputed it as it's adviced here: "There was a problem??? with reindexing process." after product import )

But that solution doesn't help at all. Please help where to search for it, I'm not really familiar with MySQL foreign keys. How to detect corrupted records? What's the principe of fixing them?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One of your products is referencing to a category_id which does not exist. Usually this should not happen when editing things through the admin-backend. Did you delete the category directly in the database?

You have to check which product is referencing the category and remove that reference. The references between category and product are stored in catalog_category_product. The categories in catalog_category_entity. To find the row you can use that query:

SELECT * FROM catalog_category_product ccp
LEFT JOIN catalog_category_entity cce ON ccp.category_id = cce.entity_id
WHERE cce.entity_id IS NULL 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...