一般错误1215:无法添加外键约束
General error 1215: cannot add foreign key constraint
我收到以下错误:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table subtags
add constraint subtags_tag_id_foreign
foreign key (tag_id
) references id
(tags
) on delete cascade)
在 Whosebug 和这篇博文中查看了有关此错误的其他答案后:https://www.percona.com/blog/2017/04/06/dealing-mysql-error-code-1215-cannot-add-foreign-key-constraint/我仍然无法弄清楚为什么会出现此错误。
这是子标签 table 的迁移:
public function up()
{
Schema::create('subtags', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('tag_id')->unsigned();
$table->integer('id')->unsigned();
$table->string('name');
$table->timestamps();
});
Schema::table('subtags', function (Blueprint $table) {
$table->foreign('tag_id')
->references('tags')
->on('id')
->onDelete('cascade');
$table->primary(array('tag_id', 'id'));
});
}
在错误点,标签 table 已经创建,我无法发现任何拼写错误,但为了确保,这里是标签 table 迁移:
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
您的查询显示您将列 & table 放错了位置:
Schema::table('subtags', function (Blueprint $table) {
$table->foreign('tag_id')
->references('tags')
->on('id')
->onDelete('cascade');
$table->primary(array('tag_id', 'id'));
});
引用 应该是“列” on “Table”喜欢:
Schema::table('subtags', function (Blueprint $table) {
$table->foreign(id'tag_id')
->references('id')
->on('tags')
->onDelete('cascade');
$table->primary(array('tag_id', 'id'));
});
您可以在迁移前禁用检查外键
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
...
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
我收到以下错误:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table
subtags
add constraintsubtags_tag_id_foreign
foreign key (tag_id
) referencesid
(tags
) on delete cascade)
在 Whosebug 和这篇博文中查看了有关此错误的其他答案后:https://www.percona.com/blog/2017/04/06/dealing-mysql-error-code-1215-cannot-add-foreign-key-constraint/我仍然无法弄清楚为什么会出现此错误。
这是子标签 table 的迁移:
public function up()
{
Schema::create('subtags', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('tag_id')->unsigned();
$table->integer('id')->unsigned();
$table->string('name');
$table->timestamps();
});
Schema::table('subtags', function (Blueprint $table) {
$table->foreign('tag_id')
->references('tags')
->on('id')
->onDelete('cascade');
$table->primary(array('tag_id', 'id'));
});
}
在错误点,标签 table 已经创建,我无法发现任何拼写错误,但为了确保,这里是标签 table 迁移:
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
您的查询显示您将列 & table 放错了位置:
Schema::table('subtags', function (Blueprint $table) {
$table->foreign('tag_id')
->references('tags')
->on('id')
->onDelete('cascade');
$table->primary(array('tag_id', 'id'));
});
引用 应该是“列” on “Table”喜欢:
Schema::table('subtags', function (Blueprint $table) {
$table->foreign(id'tag_id')
->references('id')
->on('tags')
->onDelete('cascade');
$table->primary(array('tag_id', 'id'));
});
您可以在迁移前禁用检查外键
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
...
DB::statement('SET FOREIGN_KEY_CHECKS=1;');