#1452 - Cannot Add or Update a Child Row: a Foreign Key Constraint Fails

Error 1452 MySQL - Cannot add or update Child Row

When you are trying to insert values into a table where foreign keys are defined, you need to make sure the specific values you enter for foreign key columns are available in the parent table.

In your case, when inserting values into the TruckMake and TruckModel tables, for TruckMakeID column, you are using values with the prefix TMI. But when inserting values into the Truck table, for the column TruckMakeID, you are using values with the prefix TM (I is missing). That's the reason for the error since values with prefix TM followed by a number are not available in the TruckModel table.

Since the data is not added to the Truck table, there are errors when trying to insert values to Allocation table since there is a foreign key defined for TruckVINNum referred from the Truck table.

Receving error #1452 - Cannot add or update a child row

It looks like you are trying to enforce the relationship but don't match one or more of the values that should be in the parent table. The data that relates to each other must exist in both tables, otherwise you get the error message. You'll need to clean up the data in the tables before you can enforce the relationship.

MySQL Error 1452 when inserting data

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. The FOREIGN KEY clause is specified in the child
table.

It will reject any INSERT or UPDATE operation that attempts to create
a foreign key value in a child table if there is no a matching
candidate key value in the parent table.

To know more Go to this link

So your error Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails essentially means that, you are trying to add a row to your property table for which no matching row (intid) is present in interiors table.

You must first insert the row to your interiors table.

MySQL Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails

I found the problem, I had to add the constraint

ALTER TABLE employee ADD CONSTRAINT employee_fk2 FOREIGN KEY(DeptID) REFERENCES department(DeptID) ON UPDATE CASCADE ON DELETE RESTRICT;

only after I had created both tables and inserted all the values. Thank you everyone for your suggestions.



Related Topics



Leave a reply



Submit