Search code examples
sqlconstraints

cannot add or update a child row a foreign key constraint fails for my sql database


I have created some tables in for my database. I have been struggling with this error for a few hours now and I dont know how to resolve it.

INSERT INTO ark.playerfood VALUES ('1', '3') Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (ark.playerfood, CONSTRAINT fk_PlayerFood_Player FOREIGN KEY (playerID) REFERENCES player (playerID))

[first one]1

[second one]2

[third one]3


Solution

  • well , the error message is clear , you are violating the foreign key means that its one or combination of below issue:

    1. you don't have a record in ark.player with playerid = 1
    2. and/or you don't have a record in ark.food with foodID = 3

    the error message is complaining for the first case , but I pointed out the second point as it might show up if you don't have values in there.