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

后端 未结 18 2456
谎友^
谎友^ 2020-12-05 14:15

I have an orders table and a have a sell_shipping_labels which references orders.id as a foreign. However when I run the Laravel migration I get th

相关标签:
18条回答
  • 2020-12-05 14:53

    For laravel 6+ users, I agreed with the top 2 answers its all depends on laravel versions, For the latest versions users id column uses big integer. So referencing the users id from current migration you need to use unsignedBigInteger as a reference key. Bellow is a migration example for laravel 6.5.*, Whenever we assign foreign key Keep in mind of your current laravel version

    Schema::create('galleries', function (Blueprint $table) {
            $table->bigIncrements('id');
            ==>$table->unsignedBigInteger('user_id');
            $table->string('title');
            $table->string('description');
            $table->timestamps();
            ==>$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    
    0 讨论(0)
  • 2020-12-05 14:55

    Since increments() creates an unsigned integer column, you need to define the foreign key column as unsigned integer too.

    Default migrations in Laravel 6+ use bigIncrements(), so you need to use unsignedBigInteger() method:

    $table->unsignedBigInteger('order_id');
    

    https://laravel.com/docs/6.x/migrations#foreign-key-constraints

    For default migrations in older versions of Laravel use unsignedInteger() method:

    $table->unsignedInteger('order_id');
    

    Or:

    $table->integer('order_id')->unsigned();
    

    https://laravel.com/docs/5.5/migrations#foreign-key-constraints

    0 讨论(0)
  • 2020-12-05 14:56

    If the problem is still not solved, try it. you need to create the last associated table.

    You should first create orders and after create sell_shipping_labels table

    To solve the issue you should rename migration files of Category and Users to date of before Meals Migration file that create those before Meals table.

    0 讨论(0)
  • 2020-12-05 14:57

    I faced the same problem today. My laravel version is 5.8.29. I solved the problem by doing:

    $table->bigIncrements('id'); //current table primary key and id
    $table->unsignedBigInteger('user_id'); // foreigh key
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    

    Hope this works.

    0 讨论(0)
  • 2020-12-05 15:01

    I faced this problem today. My parent table primary key datatype and child table data type was same but error was still there. I have found that my parent and child tables storage engine was different. I have fixed this issue by making both tables storage engine InnoDB from my phpmyadmin.

    0 讨论(0)
  • 2020-12-05 15:02

    I was also getting the same error. What i was doing in users table is,

    $table->unsignedInteger('role_id')->default(2); table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');

    But i have created the role table after creating users table. So, i edited the role migration file name date before the users table filename date. Like this,

    2013_01_22_091213_create_roles_table.php
    2014_10_12_000000_create_users_table.php

    And finally it works. Maybe sometime you may get this problem. So, i posted it.

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