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
目录,查看迁移文件的顺序,这将告诉您它们的执行顺序。如果顺序确实颠倒了,那么只需重命名迁移文件名开头的时间戳,使它们与您想要的顺序相匹配。