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

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

    One way to get around foreign key errors is to disable checking: "SET FOREIGN_KEY_CHECKS". This is a palliative solution, but the correct thing is really to adjust the tables and their relationships.

       DB::statement('SET FOREIGN_KEY_CHECKS=0;');
    
       Schema::table('example', function (Blueprint $table) {
    
            $table->integer('fk_example')->unsigned()->index();
            $table->foreign('fk_example')->references('id')->on('examples');
        });
    
      DB::statement('SET FOREIGN_KEY_CHECKS=1;');
    

提交回复
热议问题