mysql - Magento 和重新索引目录上的错误 1452(违反完整性约束)

标签 mysql magento

Magento 拒绝重新索引我的产品目录。它正在记录此错误:

2013-01-29T23:24:51+00:00 DEBUG (7): Exception message: SQLSTATE[23000]: 
Integrity constraint violation: 1452 Cannot add or update a child row: 
a foreign key constraint fails (`cjsquash_mgnt1`.`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`))

我认为这意味着 catalog_category_product_index 中有一个无效行,在 catalog_category_entity 表中缺少值,但我运行了这个查询:

SELECT *
FROM catalog_category_product_index
where category_id not in (select entity_id from 
catalog_category_entity)

它返回 0 行,因此看起来并非如此。我该怎么做才能解决这个问题?

最佳答案

我在仔细考虑之后自己弄明白了这一点 - 问题来自于索引过程想要在 catalog_category_product_index 表中插入一行,其中 product_id 或 category_id 不存在。这就是导致违规的原因。

如果在此步骤中失败的索引处理随后运行一个查询来确定是哪些行导致了问题,那就太好了,但在此之前,您可以运行这两个查询,它们将告诉您是哪个 product_id 或 category_id 导致了问题问题。这两个查询都应返回 0 条记录。如果他们返回一条记录,您将确切地知道哪个产品属于不存在的类别或哪个类别具有不存在的产品。你可以

SELECT * FROM `catalog_category_product` WHERE 
product_id not in (select entity_id from catalog_product_entity)

SELECT * FROM `catalog_category_product` WHERE 
category_id not in (select entity_id from catalog_category_entity)

然后您可以删除有问题的行,或者更谨慎地编辑有问题的产品或类别并重新保存其类别或产品,以便将良好的数据输入到问题表中。然后索引将起作用。

关于mysql - Magento 和重新索引目录上的错误 1452(违反完整性约束),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14594063/

相关文章:

Magento Authorize.net Direct Post 未清空购物车(未将报价设置为非事件状态)

MYSQL查询检索当前月份数据以及检查上个月

magento - 付款过程中,但订单未显示在后端?

php - 如何在 Magento 中从制造商 ID 获取制造商名称

magento - magento中捆绑产品和可配置产品之间有什么区别?

php - 用于对 magento 集合进行排序的多个字段

mysql - 升级后字符串不假设为空默认值

mysql - 在 Navicat 中重新排列列顺序

php - mysql通配符IN数据库

mysql - 如何找到导致 "Waiting for table metadata lock"状态的事务?