在Laravel迁移中丢弃外键

在Laravel迁移中丢弃外键,laravel,migration,Laravel,Migration,我在从Laravel应用程序中删除一些外键时遇到问题。问题是当我尝试回滚迁移时: php artisan migrate:rollback 我不知道为什么控制台中有错误: [照亮\数据库\查询异常] SQLSTATE[42000]:语法错误或访问冲突:1091无法删除“角色\用户\用户\ id \外部”;检查列/键是否存在(SQL:alter tablerole\u user删除外键role\u user\u user\u id\u foreign) [条令\DBAL\Driver\PDOEx

我在从Laravel应用程序中删除一些外键时遇到问题。问题是当我尝试回滚迁移时:

php artisan migrate:rollback
我不知道为什么控制台中有错误:

[照亮\数据库\查询异常] SQLSTATE[42000]:语法错误或访问冲突:1091无法删除“角色\用户\用户\ id \外部”;检查列/键是否存在(SQL:alter table
role\u user
删除外键
role\u user\u user\u id\u foreign

[条令\DBAL\Driver\PDOException] SQLSTATE[42000]:语法错误或访问冲突:1091无法删除“角色\用户\用户\ id \外部”;检查列/键是否存在

[例外情况] SQLSTATE[42000]:语法错误或访问冲突:1091无法删除“角色\用户\用户\ id \外部”;检查列/键是否存在

下面我将展示我的迁移类:

class UpdateRoleUserTable扩展了迁移
{
/**
*运行迁移。
*
*@返回无效
*/
公共职能
{
schema::table('role_user',function(Blueprint$table){
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('role_id')->引用('id')->on('roles');
});
}
/**
*反向迁移。
*
*@返回无效
*/
公共职能部门
{
Schema::table('role_user',function(Blueprint$table){
$table->dropForeign('role\u user\u user\u id\u foreign');
$table->dropForeign('role\u user\u role\u id\u foreign');
});
}
}
数据库中的“我的表”已由迁移类创建:

类CreateRoleUserTable扩展了迁移
{
/**
*运行迁移。
*
*@返回无效
*/
公共职能
{
架构::创建('role_user',函数(Blueprint$表){
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
});
}
/**
*反向迁移。
*
*@返回无效
*/
公共职能部门
{
Schema::dropIfExists('role_user');
}
}

在Laravel的所有>4.0版本中,它允许将列名放入数组中,然后自己解析。我试图找到随行的医生,但他们似乎漏掉了

在更新迁移中,请尝试以下操作:

Schema::table('role_user', function (Blueprint $table) {
  $table->dropForeign(['user_id']);
  $table->dropForeign(['role_id']);
});

我已经修改了下面的代码

onDelete()
onUpdate()添加到代码中

public function up() 
{
    Schema::table('role_user',function(Blueprint $table) {
        $table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE')->onUpdate('CASCADE');
        $table->foreign('role_id')->references('id')->on('roles')->onDelete('CASCADE')->onUpdate('CASCADE');
    });
}


我刚刚遇到了这个问题,这个问题是因为
$table->dropForeign([column_name])删除索引,而不是列本身。解决方案是在
down
函数中,将索引放在一个块中,然后将实际列放在单独的块中。它们必须在单独的块中删除,因为与不能在与要删除列的位置相同的连接中删除键有关

因此,
down
函数应该如下所示:

public function down() {
    // drop the keys
    Schema::table('role_user', function (Blueprint $table) {
        $table->dropForeign(['user_id']);
        $table->dropForeign(['role_id']);
    });

   // drop the actual columns
   Schema::table('role_user', function (Blueprint $table) {
        $table->dropColumn('user_id');
        $table->dropColumn('role_id');

    });
}

现在,您可以运行
php artisan migrate
来运行
up
函数,运行
php artisan migrate:rollback
来运行
down
命令,错误不再显示。

+1。回答得很好。你救了我。我不知道为什么在文档中这是错误的,即使在Laravel 8文档中也是如此。谢谢
public function down() {
    // drop the keys
    Schema::table('role_user', function (Blueprint $table) {
        $table->dropForeign(['user_id']);
        $table->dropForeign(['role_id']);
    });

   // drop the actual columns
   Schema::table('role_user', function (Blueprint $table) {
        $table->dropColumn('user_id');
        $table->dropColumn('role_id');

    });
}