Tuesday 29 July 2014

MySQL error: Cannot delete or update a parent row: a foreign key constraint fails"

When trying to delete table foo(DROP TABLE foo1;) I got the following error:

Cannot delete or update a parent row: a foreign key constraint fails


As root execute the following to see what in what state is the db:

SHOW ENGINE INNODB STATUS;
------------------------
LATEST FOREIGN KEY ERROR
------------------------
140729 23:16:08  Cannot drop table `mydb`.`foo1`
because it is referenced by `mydb`.`foo2`


This tells us that table foo2 has fk to foo1. So remove the fk in foo2 (set it to null) or drop foo2 (this was the case for me since foo2 was empty -- db was in an inconsistent state).

No comments:

Post a Comment