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

后端 未结 2 1866
感情败类
感情败类 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:23

    had the same problem. fixed it by adding nullable to field

    Schema::create('table_name', function (Blueprint $table) {
        ...
        $table->integer('some_id')->unsigned()->nullable();
        $table->foreign('some_id')->references('id')->on('other_table');
        ...
    });
    

    note that after migration all existed rows will have some_id = NULL

提交回复
热议问题