Error 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.

MySQL (Percona) Error 1452: Cannot add or update a child row for no reason

This is a bug. MySQL 5.7 has had some troubles with generated columns and foreign keys, and I assume this is an unfixed variant of Bug #79772 Foreign key not allowed when a virtual index exists.

In your particular case, and probably depending on your exact version, any of the following modifications seems to prevent that bug from occurring:

  • do not use a virtual column, but make it stored
  • do not create a foreign key for a column directly following the virtual column, so e.g. change the column order to year, status, createdBy_user_id, updatedBy_user_id.
  • do not use a unique index on the virtual column, a normal index should be fine (at least in a version where the linked bug is fixed). You want a unique constraint, so this is not an option, but the fact that this fixes your problem emphasized the "bug" nature of the problem.

The second bullet point seems to be the underlying bug: I assume that some iterator doesn't count the virtual column properly, so the foreign key that shall check createdBy_user_id seems to mix up the columns and actually checks the value of year (in this case "20") against the users table. So if you have a user with id "20" in your users table, the foreign key will actually accept this, no matter what value for createdBy_user_id you are trying to insert, see the MySQL 5.7.29 fiddle.

Unless you have a specific reason to use a virtual column, using stored is probably the sane thing to do.



Related Topics



Leave a reply



Submit