Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you fix error code 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.

How do you solve Cannot add or update a child row a foreign key constraint fails in MySQL?

You are getting this constraint check because Ordre table does not have reference OrdreID provided in insert command. To insert value in Ordrelinje, you first have to enter value in Ordre table and use same OrdreID in Orderlinje table. Or you can remove not null constraint and insert a NULL value in it. Hope it helps!!

How do you solve Integrity constraint violation 1452 Cannot add or update a child row a foreign key constraint fails?

If you are getting error Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails while using php artisan migrate command after creating a migration file. You can set foreign id column value as nullable with null on delete on delete parent table record.

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.


Quite likely your sourcecodes_tags table contains sourcecode_id values that no longer exists in your sourcecodes table. You have to get rid of those first.

Here's a query that can find those IDs:

SELECT DISTINCT sourcecode_id FROM 
   sourcecodes_tags tags LEFT JOIN sourcecodes sc ON tags.sourcecode_id=sc.id 
WHERE sc.id IS NULL;

I had the same issue with my MySQL database but finally, I got a solution which worked for me.
Since in my table everything was fine from the mysql point of view(both tables should use InnoDB engine and the datatype of each column should be of the same type which takes part in foreign key constraint).
The only thing that I did was to disable the foreign key check and later on enabled it after performing the foreign key operation.
Steps that I took:

SET foreign_key_checks = 0;
alter table tblUsedDestination add constraint f_operatorId foreign key(iOperatorId) references tblOperators (iOperatorId); Query
OK, 8 rows affected (0.23 sec) Records: 8  Duplicates: 0  Warnings: 0
SET foreign_key_checks = 1;

Use NOT IN to find where constraints are constraining:

SELECT column FROM table WHERE column NOT IN 
(SELECT intended_foreign_key FROM another_table)

so, more specifically:

SELECT sourcecode_id FROM sourcecodes_tags WHERE sourcecode_id NOT IN 
(SELECT id FROM sourcecodes)

EDIT: IN and NOT IN operators are known to be much faster than the JOIN operators, as well as much easier to construct, and repeat.


Truncate the tables and then try adding the FK Constraint.

I know this solution is a bit awkward but it does work 100%. But I agree that this is not an ideal solution to deal with problem, but I hope it helps.


For me, this problem was a little different and super easy to check and solve.

You must ensure BOTH of your tables are InnoDB. If one of the tables, namely the reference table is a MyISAM, the constraint will fail.

    SHOW TABLE STATUS WHERE Name =  't1';

    ALTER TABLE t1 ENGINE=InnoDB;

This also happens when setting a foreign key to parent.id to child.column if the child.column has a value of 0 already and no parent.id value is 0

You would need to ensure that each child.column is NULL or has value that exists in parent.id

And now that I read the statement nos wrote, that's what he is validating.


I had the same problem today. I tested for four things, some of them already mentioned here:

  1. Are there any values in your child column that don't exist in the parent column (besides NULL, if the child column is nullable)

  2. Do child and parent columns have the same datatype?

  3. Is there an index on the parent column you are referencing? MySQL seems to require this for performance reasons (http://dev.mysql.com/doc/refman/5.5/en/create-table-foreign-keys.html)

  4. And this one solved it for me: Do both tables have identical collation?

I had one table in UTF-8 and the other in iso-something. That didn't work. After changing the iso-table to UTF-8 collation the constraints could be added without problems. In my case, phpMyAdmin didn't even show the child table in iso-encoding in the dropdown for creating the foreign key constraint.