Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql foreign key error #1452

Tags:

mysql

ALTER TABLE  `groups` ADD FOREIGN KEY (  `company_id` ) REFERENCES  `summaries`.`companies` (

`id`
) ON DELETE CASCADE ;

MySQL said: 

#1452 - Cannot add or update a child row: a foreign key constraint fails (`summaries/#sql-164a_33c`, CONSTRAINT `#sql-164a_33c_ibfk_1` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`) ON DELETE CASCADE) 

companies.id is primary auto increment int(11)

company_id is index int(11)

I don't understand the error message. Can anyone shed some light on this?

like image 286
Webnet Avatar asked Mar 04 '11 19:03

Webnet


People also ask

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 does it mean when 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.

What is a foreign key violation?

Foreign key constraint violation occurred, dbname = <database_name> , table name = <table_name> , constraint name = <constraint_name> . 23000. Occurs when an insert or update on a foreign key table is performed without a matching value in the primary key table.

Can you update a foreign key in MySQL?

Here is how you would do that: ALTER TABLE my_table ADD FOREIGN KEY (key) REFERENCES other_table(id) ON DELETE SET NULL; And that's it!! That's how you change a foreign key constraint in MySQL!


2 Answers

That means you have at least one row in the child table that references a non-existent row in the parent table.

If you are absolutely sure that you are okay with having a data integrity issue like that, you can add the foreign key by disabling foreign key checks before you run the ALTER TABLE command:

SET FOREIGN_KEY_CHECKS = 0;
like image 72
Ike Walker Avatar answered Oct 03 '22 16:10

Ike Walker


I just had this problem, although in a somewhat more specific scenario.

In my case, I had added, to an existing table, a column that I needed to be both nullable and act as a foreign key (i.e., for non-null entries to be bound by a foreign key constraint).

The referenced column looked like this:

+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| id          | int(10)     | NO   | PRI | NULL    | auto_increment |
+-------------+-------------+------+-----+---------+----------------+

and the referencing one like this:

+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| bed_id      | int(10)     | YES  |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+

Turned out that I had forgotten to specify DEFAULT NULL when adding the referencing column to the existing table and so it was automatically filled with zeros, which failed the foreign key constraint.

I changed them to NULL:

update devices set bed_id = NULL where bed_id = 0;

and then successfully added the foreign key constraint. Hope this helps someone

like image 25
mcmlxxxvi Avatar answered Oct 03 '22 16:10

mcmlxxxvi