DEV Community

Yasunori Tanaka
Yasunori Tanaka

Posted on

1

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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay