The INSERT statement conflicted with the FOREIGN KEY constraint "XXX". The conflict occurred in database "XXX", table "XXX", column "XXX" error and how to fix it

It is very common to do migrations of data from a table of one database to another or inserting backed-up data into a table. For these tasks you will have to use long .sql scripts with INSERT statements.

When doing such migrations one possible problem might be that the Foreign Keys referenced in the primary-table will throw the INSERT statement conflicted with the FOREIGN KEY constraint “XXX. The conflict occurred in database “”, table “”, column “” error.

The reason for the error is that you are trying to insert a value or values in the Foreign Key column of the primary-table that do not exist on the foreign-table. The names of the foreign column and table are mentioned in the error message.

You will have to check the values of the specified column inside your .sql script and correct any values that do not appear in the foreign-table. Either add these values in the foreign-table or delete them in the .sql script :)

comments powered by Disqus