Why do i see 'cannot add or update a child row: a foreign key constraint fails' in sql?
Example:
INSERT INTO orders (order_id, user_id) VALUES (1, 5);
You're trying to insert or update a record in a child table for which there's no corresponding record in the parent table.
Solution:Ensure that the value you're inserting/updating in the child table has a corresponding record in the parent table.
-- Ensure the user exists:
INSERT INTO users (id, name) VALUES (5, 'John');
-- Then, add the order:
INSERT INTO orders (order_id, user_id) VALUES (1, 5);