![](/img/trans.png)
[英]ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails That I can't fix
[英]How to fix error in MySQL : 1452 - Cannot add or update a child row: a foreign key constraint fails
我在 Navicat 中運行 sql 查詢,所以出現錯誤;
詢問:
ALTER TABLE `customer_eav_attribute`
ADD CONSTRAINT `CUSTOMER_EAV_ATTRIBUTE_ATTRIBUTE_ID_EAV_ATTRIBUTE_ATTRIBUTE_ID`
FOREIGN KEY (`attribute_id`)
REFERENCES `eav_attribute` (`attribute_id`)
ON DELETE CASCADE;
錯誤:
1452 - Cannot add or update a child row: a foreign key constraint
fails (`caterin1_test`.`#sql-dd4_13`, CONSTRAINT
`CUSTOMER_EAV_ATTRIBUTE_ATTRIBUTE_ID_EAV_ATTRIBUTE_ATTRIBUTE_ID`
FOREIGN KEY (`attribute_id`)
REFERENCES `eav_attribute` (`attribute_id`)
ON DELETE CA)
我該如何解決?
我認為 eav_attribute 表和 customer_eav_attribute 表中沒有鏈接 id。 您必須檢查 eav_attribute 表和 customer_eav_attribute 表(最好的方法:請刪除 eav_attribute 和 customer_eav_attribute 然后重新插入數據)。 你可以找到解決方案。
當您嘗試將源文件導入現有數據庫時,通常會發生這種情況。 刪除兩個表(customer_eav_attribute,eav_attribute)。 請重新創建表。
CREATE TABLE t1
(id INTEGER);
CREATE TABLE t2
(t1_id INTEGER,
CONSTRAINT FOREIGN KEY (t1_id) REFERENCES t1 (id));
然后這樣設置。
ALTER TABLE `customer_eav_attribute`
ADD CONSTRAINT `CUSTOMER_EAV_ATTRIBUTE_ATTRIBUTE_ID_EAV_ATTRIBUTE_ATTRIBUTE_ID`
FOREIGN KEY (`attribute_id`)
REFERENCES `eav_attribute` (`attribute_id`)
ON DELETE CASCADE;
在定義外鍵引用它之前,父表必須存在。 您必須以正確的順序定義表:首先是父表,然后是子表。 如果兩個表相互引用,則必須創建一個沒有 FK 約束的表,然后創建第二個表,然后使用 ALTER TABLE 將 FK 約束添加到第一個表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.