Laravel 在迁移中删除外键
Laravel drop foreign Key in Migration
我想创建一个 Migration,它将删除一个 table。我这样创建了迁移:
Schema::table('devices', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('client_id')->nullable();
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
});
现在我试着这样放下它:
Schema::table('devices', function (Blueprint $table) {
$table->dropForeign('devices_client_id_foreign');
$table->drop('devices');
});
但是我得到以下错误:
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:
alter table devices
drop foreign key 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
只需放下整个 table (documentation):
Schema::drop('devices');
试试这个方法...
public function down()
{
Schema::dropIfExists('devices');
}
//Or this
public function down(){
Schema::table('devices', function (Blueprint $table) {
$table->dropForeign(['client_id']);
$table->dropColumn('client_id');
$table->drop('devices');
});
}
您只需要在删除 table 之前禁用外键检查,然后像这样再次启用它们:
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::dropIfExists('devices');
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
你可以使用这个答案。
将列名作为数组传递给 dropForeign。在内部,Laravel 删除关联的外键。
$table->dropForeign(['client_id']);
我想创建一个 Migration,它将删除一个 table。我这样创建了迁移:
Schema::table('devices', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('client_id')->nullable();
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
});
现在我试着这样放下它:
Schema::table('devices', function (Blueprint $table) {
$table->dropForeign('devices_client_id_foreign');
$table->drop('devices');
});
但是我得到以下错误:
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:
alter table
devices
drop foreign keydevices_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
只需放下整个 table (documentation):
Schema::drop('devices');
试试这个方法...
public function down()
{
Schema::dropIfExists('devices');
}
//Or this
public function down(){
Schema::table('devices', function (Blueprint $table) {
$table->dropForeign(['client_id']);
$table->dropColumn('client_id');
$table->drop('devices');
});
}
您只需要在删除 table 之前禁用外键检查,然后像这样再次启用它们:
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::dropIfExists('devices');
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
你可以使用这个答案。
将列名作为数组传递给 dropForeign。在内部,Laravel 删除关联的外键。
$table->dropForeign(['client_id']);