Laravel放弃迁移中的外键 [英] Laravel drop foreign Key in Migration
本文介绍了Laravel放弃迁移中的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个将删除表的迁移.我这样创建了迁移:
I want to create a Migration which shall drop a table. I created the Migration like this:
Schema::table('devices', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('client_id')->nullable();
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
});
现在我尝试像这样删除它:
And now I try to drop it like this:
Schema::table('devices', function (Blueprint $table) {
$table->dropForeign('devices_client_id_foreign');
$table->drop('devices');
});
但是我收到以下错误消息:
But I get following error:
In Connection.php line 664:
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'devices_client_id_foreign'; check that column/key exists (SQL:
更改表devices
删除外键devices_client_id_foreign
)
In PDOStatement.php line 144:
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'devices_client_id_foreign'; check that column/key exists
In PDOStatement.php line 142:
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'devices_client_id_foreign'; check that column/key exists
推荐答案
您只需要在删除表之前禁用外键检查,然后按如下所示再次启用外键检查:
You just need to disable foreign key checks before you drop the table then enable them again after like this:
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::dropIfExists('devices');
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
这篇关于Laravel放弃迁移中的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文