SQLSTATE[HY000]: General error: 1215 - Even with 'InnoDB' and unsigned

SQLSTATE[HY000]: General error: 1215 - Even with 'InnoDB' and unsigned

我正在尝试在从 domains table 到 countriesid 字段的 country_id 字段上创建一个外键。但是当我进行迁移时:php artisan migrate:fresh.

错误信息:

Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `domains` add constraint `domains_country_id_foreign` foreign key (`country_id`) references `countries` (`id`) on delete cascade)

这是 domains 迁移文件:

public function up()
{
    Schema::create('domains', function (Blueprint $table) {
        $table->increments('id');
        $table->string('code');
        $table->string('name');
        $table->string('display_name');
        $table->string('extension');
        $table->string('google_analytics')->nullable();
        $table->string('google_webmastertool')->nullable();
        $table->string('google_maps')->nullable();
        $table->integer('country_id')->unsigned();
        $table->boolean('actif')->default(true);
        $table->timestamps();
        $table->engine = 'InnoDB';
    });

    Schema::table('domains', function (Blueprint $table) {
        $table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');
    });
}

这里是 countries 迁移文件:

public function up()
{
    Schema::create('countries', function (Blueprint $table) {
        $table->increments('id');
        $table->string('code');
        $table->string('name');
        $table->string('alpha2', 2);
        $table->string('alpha3', 3);
        $table->timestamps();
        $table->engine = 'InnoDB';
    });
}

如您所见,country_id 字段是无符号的,table 引擎是 InnoDB。我已经用外键完成了其他迁移并且它们工作正常,但是这个不起作用:|

在此先感谢您的帮助!

来自@Bogdan 的解决方案。

使用迁移文件的时间戳更改迁移顺序。 您需要让带有外键的文件具有比主键所在的迁移文件的时间戳更高的时间戳