I’m trying to INSERT into a table, but I get the error Cannot add or update a child row: a foreign key contraint fails.
And as the error says, I have a foreign key I’m trying to update.
Right now I’m using a simple INSERT like:
INSERT INTO tblTable1 (firstColumn, foreignKey) VALUES ('blabla', 1)
How can I insert this if foreignKey is the id of tblTable2?
You need to add the record to the parent table first, then the child table.
From MySQL documentation on foreign key constraints:
Here’s MySQL documentation of how to use foreign key constraints and examples.