Laravel migration - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

后端 未结 2 1858
感情败类
感情败类 2020-12-17 19:05

I am trying to run a migration for a table inventories that I have created with this migration:

Schema::         


        
2条回答
  •  隐瞒了意图╮
    2020-12-17 19:40

    You probably have some records in the inventories table with local_id that does not have corresponding id in the contents table, hence the error. You could solve it by one of the two ways:

    • Run the migration with foreign_key_checks turned off. This will disabled the foreign key constraints for the existing rows (if that's what you want). It's documented here
    • Insert only those records that have corresponding id field in contents table. You can use INSERT INTO.. WHERE EXISTS query to filter the records out, and insert only those records.

提交回复
热议问题