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

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

    You should first create Categories and users Table when create "meals"

    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.


    sample:

    2019_04_10_050958_create_users_table 
    2019_04_10_051958_create_categories_table
    2019_04_10_052958_create_meals_table
    
    0 讨论(0)
  • 2020-11-30 07:42

    Remember that this is important the referenced and referencing fields must have exactly the same data type.

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

    @JuanBonnett’s question has inspired me to find the answer. I used Laravel to automate the process without considering the creation time of the file itself. According to the workflow, “meals” will be created before the other table (categories) because I created its schema file (meals) before categories. That was my fault.

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

    You should create your migration in order for example I want my users to have a role_id field which is from my roles table

    I first start to make my role migration php artisan make:migration create_roles_table --create=roles

    then my second user migration php artisan make:migration create_users_table --create=users

    php artisan migration will execute using the order of the created files 2017_08_22_074128_create_roles_table.php and 2017_08_22_134306_create_users_table check the datetime order, that will be the execution order.

    files 2017_08_22_074128_create_roles_table.php

    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50);
            $table->timestamps();
        });
    }
    

    2017_08_22_134306_create_users_table

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('role_id')->unsigned();
            $table->string('name');
            $table->string('phone', 20)->unique();
            $table->string('password');
            $table->rememberToken();
            $table->boolean('active');
            $table->timestamps();
            $table->foreign('role_id')->references('id')->on('roles');
        });
    }
    
    0 讨论(0)
  • 2020-11-30 07:46

    In my case the problem was that one of the referenced tables was InnoDB and the other one was MyISAM.

    MyISAM doesn't have support for foreign key relations.

    So, now both tables are InnoDB. Problem solved.

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

    Just add ->unsigned()->index() at the end of the foreign key and it will work.

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