Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a record when two tables have foreign key referenced to each other?

Delete any record of them will report an error like this:

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails

How to overcome this problem?

like image 414
user198729 Avatar asked Dec 12 '22 23:12

user198729


1 Answers

Pick a record which allows nulls to be inserted in a FK column, insert it there, delete other record and then delete the first one.

like image 190
Anton Gogolev Avatar answered Dec 25 '22 22:12

Anton Gogolev