对于此错误消息,mysqlimport是否有办法输出未通过约束的特定行?这对调试很有帮助。否则,我不知道哪一行有这个问题。
mysqlimport: Error: 1452, Cannot add or update a child row: a foreign key constraint fails (`bwordDS`.`Category_Term`, CONSTRAINT `FK_qn1crq26sr0hqm5q2p7nfvdu1` FOREIGN KEY (`categories_id`) REFERENCES `Category` (`id`)), when using table: Category_Term
答案 0 :(得分:2)
首先检查是否存在您尝试导入此数据的类别表。
如果存在Category表,则需要检查此表中的所有category_id是否应作为id存在于Category表中。
更好的选择是导入第一个类别表然后导入此表。
在通用的第一个所有父表中,数据应该导入子表。
肮脏的方式如下所示,不推荐 -
set foreign_key_checks=0;
import data here;
set foreign_key_checks=1;
只是知道哪一行正在创建问题 -
以下查询将为您提供有问题的行。
SELECT a.category_id FROM Category_Term a
LEFT JOIN Category b ON a.category_id=b.id
WHERE b.id IS NULL;
注意:假设category_term中的category_id和类别表中的id将被编入索引。
答案 1 :(得分:1)
我使用了@Zafar提供的示例查询,但我也添加了DISTINCT
:
SELECT DISTINCT contries.city_id
FROM contries
LEFT JOIN cities ON contries.city_id=city.id
WHERE city.id IS NULL;