0

Hi I've been trying to code this

ALTER TABLE `inventory_slit` ADD  CONSTRAINT `qualitycontrol_username_slit` FOREIGN KEY (`qualitycontrol_username`) REFERENCES `qualitycontrol_info`(`qualitycontrol_username`) ON DELETE RESTRICT ON UPDATE RESTRICT;

But the result is :

#1452 - Cannot add or update a child row: a foreign key constraint fails (`dcpc`.`#sql-434_597`, CONSTRAINT `qualitycontrol_username_slit` FOREIGN KEY (`qualitycontrol_username`) REFERENCES `qualitycontrol_info` (`qualitycontrol_username`))

That was said by the MYSQL. Can you help me how to fix this?

  • @PaulSpiegel Agree, this is a duplicate. However, the answer that has the most votes suggests deleting records that don't have the matching key in the referenced table. That will definitely work but, given that the foreign key wasn't in place to enforce referential integrity before the records were created, the solution here might be to UPDATE the referencing rows to fix a typo or even INSERT a missing record in the referenced table rather than going for the nuclear option and DELETE rows which might then be difficult to recreate. – Paul Campbell Feb 27 '18 at 18:06
  • @PaulCampbell The referenced answer explains why. There are also other answers, which show how to find the problematic rows. Fixing the data is up to OP. We can't help without more details. – Paul Spiegel Feb 27 '18 at 18:28

0 Answers0