DEV Community

Yasunori Tanaka
Yasunori Tanaka

Posted on

Look into foreign key constraint error in MySQL

Sometimes we encounter the error that we cannot insert/update/delete records by foreign key constraint error.

The error message like

[23000][1452] Cannot add or update a child row: a foreign key constraint fails

But this message may not help us to solve the problem because it doesn't have the detail.

So we can see the detail with the below query.

mysql> show engine innodb status;

Then we can get the detail in "LATEST FOREIGN KEY ERROR" section.

------------------------
LATEST FOREIGN KEY ERROR
------------------------
2020-08-05 07:37:15 0x7fe488261700 Transaction:
TRANSACTION 306127, ACTIVE 0 sec updating or deleting
mysql tables in use 1, locked 1
...

Top comments (0)