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

后端 未结 22 897
有刺的猬
有刺的猬 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:50

    Maybe it can be of help to anyone landing here : I just experienced this same issue, and in my case it was that I had a (composite) unique constraint set on the foreign key column BEFORE the foreign key constraint. I resolved the issue by having the "unique" statement placed AFTER the "foreign" statement.

    Works:

    $table->foreign('step_id')->references('id')->on('steps')->onDelete('cascade');
    $table->unique(['step_id','lang']);
    

    Doesn't work:

    $table->unique(['step_id','lang']);
    $table->foreign('step_id')->references('id')->on('steps')->onDelete('cascade');
    

提交回复
热议问题