一般错误:1005 无法创建表,laravel 中的外键约束格式不正确

Afs*_*adi 3 laravel laravel-5.2

为什么 Laravel 架构回复

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table test。#sql-13cc_d0(errno: 150 "外键约束的格式不正确") (SQL:删除级联上的alter table citiesadd constraintcities_provinces_id_foreign外键( provinces_id) 引用provinces( id))

[PDOException] SQLSTATE[HY000]:一般错误:1005 无法创建表test。#sql-13cc_d0(errno: 150 "外键约束的格式不正确")

第一张桌子

Schema::create('provinces', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->boolean('is_enable');
        $table->boolean('is_deletable');
        $table->boolean('is_editable');
        $table->boolean('deleted');
        $table->timestamps();
    }); 
Run Code Online (Sandbox Code Playgroud)

第二张桌子

    Schema::create('cities', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('province_id')->unsigned()->index();
        $table->boolean('is_enable');
        $table->boolean('is_deletable');
        $table->boolean('is_editable');
        $table->boolean('deleted');
        $table->timestamps();


        $table->foreign('province_id')
            ->references('id')->on('provinces')
            ->onDelete('cascade');

    });
Run Code Online (Sandbox Code Playgroud)

Ami*_*pta 5

您应该确保provinces表迁移在表迁移之前运行cities。

您应该更改provinces或cities迁移文件名,以确保provinces表迁移文件开头的时间戳位于cities表之前。


小智 5

时间继续,但我会评论别的东西。

安迪尔是对的!您必须具有相同的类型。在这种情况下,外键应该具有类型,bigInteger因为您bigIncrements在引用表中具有。

我认为这就是一些无聊的调试的解决方案。(对不起我的英语不好)