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

后端 未结 22 714
有刺的猬
有刺的猬 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;');
    
    0 讨论(0)
  • 2020-11-30 07:53

    Laravel 5.8

    In the foreign key column use unsignedBigInteger to avoid mismatch foreign key data type problem . For example let us assume we have two tables questions and replies
    Questions table will look:

     public function up()
        {
            Schema::create('questions', function (Blueprint $table) {
                $table->bigIncrements('id');
                 $table->text('body');
                 $table->integer('user_id')->unsigned();
                $table->timestamps();
            });
        }
    

    Replies table look like :

    public function up()
    {
        Schema::create('replies', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('body');
            $table->unsignedBigInteger('question_id');
            $table->integer('user_id')->unsigned();
            $table->foreign('question_id')->references('id')->on('questions')->onDelete('cascade');
            $table->timestamps();
        });
    }
    
    0 讨论(0)
  • 2020-11-30 07:54

    I had to face the same problem at Larvel 6. I solve this following way.

    I think it helps you or others:

    $table->increments('id');
        $table->bigInteger('user_id')->unsigned(); //chnage this line
        $table->bigInteger('category_id')->unsigned(); //change this line
        ---
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');
        $table->foreign('category_id')
            ->references('id')
            ->on('categories')
            ->onDelete('cascade');
    

    Incrementing ID using a "big integer" equivalent.

    used bigInteger instead of Integer

    1. If still now you got an error.

    I suggest you reorder your migration file following ways:

    Change the dates that form the first part of the migration filenames So they're in the order you want (example: for 2020_07_28_133303_update_categories.php, the date & time is 2020-07-28, 13:33:03);

    N.B: First must be 'categories' migration file than 'meals' migration File.

    0 讨论(0)
  • 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');
    
    0 讨论(0)
  • 2020-11-30 07:58

    The order of creation of migration files should be sorted and the foreign key should have exactly similar property as the primary key in the other table.

    0 讨论(0)
  • 2020-11-30 07:59

    Migrations must be created top-down.

    First create the migrations for the tables who don't belong to anyone.

    Then create the migrations for tables that belong to the previous.


    Simplified answer to the table engine problem:

    To set the storage engine for a table, set the engine property on the schema builder:

    Schema::create('users', function ($table) {
        $table->engine = 'InnoDB';
    
        $table->increments('id');
    });
    

    From Laravel Docs: https://laravel.com/docs/5.2/migrations

    0 讨论(0)
提交回复
热议问题