Reputation: 175
I'm trying to link 2 tables together in the database I created. but I'm getting #1452 - Cannot add or update a child row error.
interestingly, it allows me to establish a fk-pk relationship when the table is empty, but I get an error when I pass data to the table with csv. when I first set up relationships and then try to import the data, I get the error adding this time.
what would be the reason?
SQL Query:
ALTER TABLE `kickstarter` ADD FOREIGN KEY (`country_num`) REFERENCES `kickstarter_countries`(`country_id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
MySQL Output:
1452 - Cannot add or update a child row: a foreign key constraint fails (
htimurat_data_analytics
.#sql-f8c5_6a687
, CONSTRAINT#sql-f8c5_6a687_ibfk_1
FOREIGN KEY (country_num
) REFERENCESkickstarter_countries
(country_id
))
Upvotes: 1
Views: 5956
Reputation: 12422
It looks like you are trying to enforce the relationship but don't match one or more of the values that should be in the parent table. The data that relates to each other must exist in both tables, otherwise you get the error message. You'll need to clean up the data in the tables before you can enforce the relationship.
Upvotes: 2