Laravel migration: “Foreign key constraint is incorrectly formed” (errno 150)

后端 未结 22 919
有刺的猬
有刺的猬 2020-11-30 07:21

When migrating my DB, this error appears. Below is my code followed by the error that I am getting when trying to run the migration.

Code



        
22条回答
  •  甜味超标
    2020-11-30 07:58

    if you are using ->onDelete('set null') in your foreign key definition make sure the foreign key field itself is nullable() ie

    //Column definition
    $table->integer('user_id')->unsigned()->index()->nullable(); //index() is optional
    
    //...
    //...
    
    //Foreign key 
    $table->foreign('user_id')
          ->references('id')
          ->on('users')
          ->onDelete('set null');
    

提交回复
热议问题