Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel SQLSTATE[HY000]:一般错误:1215-即使使用';InnoDB';而且没有签名_Laravel_Laravel 5_Laravel Migrations - Fatal编程技术网

Laravel SQLSTATE[HY000]:一般错误:1215-即使使用';InnoDB';而且没有签名

Laravel SQLSTATE[HY000]:一般错误:1215-即使使用';InnoDB';而且没有签名,laravel,laravel-5,laravel-migrations,Laravel,Laravel 5,Laravel Migrations,我正在尝试在国家/地区id字段上创建一个外键,从域表到国家/地区的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_i

我正在尝试在
国家/地区id
字段上创建一个外键,从
表到
国家/地区的
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)
以下是
迁移文件:

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');
    });
}
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';
    });
}
这是
国家
迁移文件:

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');
    });
}
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\u id
字段是无符号的,表引擎是InnoDB。我已经用外键进行了其他迁移,它们工作得很好,但这一次不行:|

提前感谢您的帮助

来自@Bogdan的解决方案

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

我猜
国家
迁移是在
之前运行的。还有,为什么在
闭包中使用外键定义而不是
创建
闭包中使用外键定义?我使用的答案是:。对于
而不是
创建
,但在我在
创建
方法中设置外键之前。相同的错误:/但是
国家
迁移是否在
迁移之前运行?我不知道,但我认为是这样,因为这是发生此错误的唯一方法。。。您知道我可以在哪里或如何管理它吗?请查看您的
数据库/migrations
目录,查看迁移文件的顺序,这将告诉您它们的执行顺序。如果顺序确实颠倒了,那么只需重命名迁移文件名开头的时间戳,使它们与您想要的顺序相匹配。