MySQL said: Cannot add or update a child row: a foreign key constraint fails
When working with MySQL InnoDB relationships, I ran into a snag when dealing with foreign keys. I was attempting to creating a cascading relationship between a “projectID” and the “ID” field of the “projects” table. BUT, I kept getting an annoying error stating: “MySQL said: Cannot add or update a child row: a foreign key constraint fails”.
After 30 minutes of failed Google searches, I finally figured out the issue myself.
Problem:
Attempting to create a foreign key cascade relationship between tables failed. MySQL simply said NO, Scott, you’re DUMB!
Solution:
I was attempting to relate a foreign key in one table to a primary key in another table where a matching row did not exist. In order to create a cascading foreign key relationship from one table to the next, you cannot have records in the foreign table without a corresponding record in the primary table.
For example: if I have a taskList with a foreign key for “projectID” with a value of 3, I must have a record in the table for “projects” with a matching “ID” of 3.
Hope this helps you keep your hands out of your hair!





