Search code examples
mysqlforeign-keys

How to fix error in MySQL : 1452 - Cannot add or update a child row: a foreign key constraint fails


I run sql query in Navicat, so got error;

Query:

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;

Error:

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)

How can I fix it?


Solution

  • I think there is no linked id in eav_attribute table and customer_eav_attribute table. You must check eav_attribute table and customer_eav_attribute table (best way: plz delete eav_attribute and customer_eav_attribute and then insert data again). You can find solution.