Laravel 在迁移中删除外键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51860723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:58:42  来源:igfitidea点击:

Laravel drop foreign Key in Migration

phpmysqllaravellaravel-5.6

提问by Jon not doe xx

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:

alter table devicesdrop 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
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

采纳答案by Jason Grim

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;');

回答by gandarrillas

You can use this answer. https://stackoverflow.com/a/30177480/8513937

你可以使用这个答案。https://stackoverflow.com/a/30177480/8513937

Pass to dropForeignthe column name as array. Internally, Laravel drops the associated foreign key.

将列名作为数组传递给dropForeign。在内部,Laravel 删除关联的外键。

$table->dropForeign(['client_id']);

回答by Jonas Staudenmeir

Just drop the whole table (documentation):

只需删除整个表(文档):

Schema::drop('devices');

回答by Laurazel8

Try this ways...

试试这些方法...

  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');
    });
  }