I have a clients, feedback and a repairs tables. A client can give many feedbacks and have many repairs. In the feedback table I have created a clientid column (added also index) and I am able to create a foreign key to the clientid column (primary key) of the clients table.
The problem is that I am unable to do the same with the repairs table. Even though I have created a clientid column (indexed) within the repairs table and it has the same properties as the clientid within the clients table I get the following:
MySQL said: Documentation
#1452 – Cannot add or update a child row: a foreign key constraint
fails (ccsdb.,
CONSTRAINT#sql-3f0_8e5_ibfk_1FOREIGN KEY (client_id) REFERENCES
clients(client_id) ON DELETE CASCADE ON UPDATE CASCADE)
That error (
Cannot add or update a child row: a foreign key constraint fails) is referenced in the MySQL FK DocTo add a reference between 2 tables the condition must fit for existing data.
That means if you say
table1.id = table2.idthen all ids oftable1andtable2must match together.To solve that you have to eliminate or fix those rows that don’t match.
Example: