Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1452 - Cannot add or update a child row: a foreign key constraint fails

I'm getting this error:

1452 - Cannot add or update a child row: a foreign key constraint fails.

I've located them and try to get rid of the references with

alter table tillhör drop foreign key kat_id;

But getting this error instead:

#1025 - Error on rename of '.\recept\tillh@1ir' to '.\recept#sql2-1570-3cb' (errno: 152).

What do I do wrong?

like image 820
Mia Raunegger Avatar asked Sep 23 '13 17:09

Mia Raunegger


People also ask

How do you fix error 1452 Cannot add or update a child row a foreign key constraint fails?

There are two ways you can fix the ERROR 1452 in your MySQL database server: You add the value into the referenced table. You disable the FOREIGN_KEY_CHECKS in your server.

Can't add or update a child row a foreign key constraint fails?

The error comes when you are trying to add a row for which no matching row in in the other table. “Foreign key relationships involve a parent table that holds the central data values, and a child table with identical values pointing back to its parent.

How do I fix foreign key constraint failure?

The error message itself showing there is a foreign key constraint error, which means you are deleting a parent table where the child table contains the Primary table identifier as a foreign key. To avoid this error, you need to delete child table records first and after that the parent table record.

What is child row?

Description. DataTables has the ability to show child rows for each row (termed a "parent row" in this documentation to distinguish from the child rows). This child rows are attached to each parent row, and can be used, for example, to provide extra information about the parent row, or an editing form.


3 Answers

Before you query run

SET FOREIGN_KEY_CHECKS=0

then set it to 1 after you`re done.

like image 107
Mihai Avatar answered Oct 17 '22 17:10

Mihai


I face same problem. I solve this issue by clearing, i.e. deleting all data from child table and successfully done.

This is occur if child table contain some data with the foreign key that that are not in parent table i.e, if there are two table called Person (with column id, name, address) and order(with column id, person_id, order_name); order.person_id is foreign key of person.id and order table contain person_id that is not present in person table.

You can solve this using the following query

Delete from order where person_id NOT IN (select id from person where person.id = order.person_id)
like image 31
Jakir Hosen Khan Avatar answered Oct 17 '22 18:10

Jakir Hosen Khan


When I had this problem, it was due to the fact that I forgot to specify NULLS allowed when creating the foreign key id fields. I changed it after the fact, but 0's were already in the value. It could not find 0 in the matching table, and then gave this error. The fix: update the zero values to nulls.

like image 33
Cymbals Avatar answered Oct 17 '22 17:10

Cymbals