在Laravel迁移中删除外键 [英] Dropping foreign keys in Laravel migration

查看:283
本文介绍了在Laravel迁移中删除外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem with dropping some foreign keys from my Laravel application. The problem is when I am trying to rollback the migration:

php artisan migrate:rollback

我不知道为什么控制台中出现错误:

I don't know why I have errors in the console:

[Illuminate \ Database \ QueryException] SQLSTATE [42000]:语法错误或访问冲突:1091无法删除'role_user_user_id_foreign';检查是否存在列/键(SQL:更改表role_user删除外键role_user_user_id_foreign)

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'role_user_user_id_foreign'; check that column/key exists (SQL: alter table role_user drop foreign key role_user_user_id_foreign)

[Doctrine \ DBAL \ Driver \ PDOException] SQLSTATE [42000]:语法错误或访问冲突:1091无法删除'role_user_user_id_foreign';检查列/键是否存在

[Doctrine\DBAL\Driver\PDOException] SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'role_user_user_id_foreign'; check that column/key exists

[PDOException] SQLSTATE [42000]:语法错误或访问冲突:1091无法删除'role_user_user_id_foreign';检查列/键是否存在

[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'role_user_user_id_foreign'; check that column/key exists

下面我将展示我的迁移课程:

Below I'm showing my migration class:

class UpdateRoleUserTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        schema::table('role_user',function(Blueprint $table){


            $table->foreign('user_id')->references('id')->on('users');
            $table->foreign('role_id')->references('id')->on('roles');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('role_user', function (Blueprint $table) {
        $table->dropForeign('role_user_user_id_foreign');
        $table->dropForeign('role_user_role_id_foreign');

    });
    }
}

我的数据库表已经由迁移类创建:

My table in the database has been created by migration class:

class CreateRoleUserTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('role_user', function (Blueprint $table) {

            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('role_id')->unsigned();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('role_user');
    }
}

推荐答案

在所有> 4.0版本的Laravel中,它允许将列名放入数组中,然后数组将自行解析.我试图找到随附的文档,但他们似乎已将其遗漏了.

In all of the >4.0 versions of Laravel, it allows placing column names into an array, which it will then resolve on its own. I tried to find accompanying docs, but they seem to have left it out.

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

In your update migration, try this:

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

这篇关于在Laravel迁移中删除外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆