Fat Morph
Fat Morph

Reputation: 9

MySQL Error 1452: Cannot add or update a child row

First of all, sorry for this being in Dutch, I doubt understanding the language is needed for this though.

whenever I Forward-Engineer, it gives me error 1452. The thing is, I am using existing parent table data to fill the child table.

These are the two tables i'm having trouble with.

This is the insert from the PARENT table, which is not empty as you can see.

This is the insert from the CHILD table, filled with existing data of PARENT table.

And finally the error I get when forward-engineering here.

I've been stuck for so long, would really appreciate an answer. Hope I gave enough information (first time asking question).

Upvotes: 0

Views: 630

Answers (1)

Lakshan Dissanayake
Lakshan Dissanayake

Reputation: 561

Mysql 1452 error refers to that you're trying to insert child records whose parent id(s) are not in the parent table.

try matching your columns with the following query.

SELECT 
    DISTINCT ct.id 
FROM 
   child_table as ct 
   LEFT JOIN parent_table as pt ON ct.parent_id=pt.id 
WHERE 
    pt.id IS NULL;

Hope this helps!

Upvotes: 2

Related Questions