在Laravel迁移中删除外键

p3rjfoxz  于 2022-12-19  发布在  其他
关注(0)|答案(4)|浏览(149)

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

php artisan migrate:rollback

我不知道为什么控制台中出现错误:
[说明\数据库\查询异常] SQLSTATE[42000]:语法错误或访问冲突:1091无法删除“角色用户用户标识外部”;检查列/键是否存在(SQL:更改表role_user删除外键role_user_user_id_foreign
[教义\数据库应用程序\驱动程序\PDF异常] SQLSTATE[42000]:语法错误或访问冲突:1091无法删除“角色用户用户标识外部”;检查列/键是否存在
[PDF异常] SQL状态[42000]:语法错误或访问冲突:1091无法删除“角色用户用户标识外部”;检查列/键是否存在
下面是我的迁移类:

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

    });
    }
}

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

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

vjrehmav1#

在Laravel的所有〉4.0版本中,它允许将列名放入数组中,然后它将自己解析该数组。我试图找到附带的文档,但他们似乎将其遗漏了。
在更新迁移中,请尝试以下操作:

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

q3qa4bjr2#

我刚刚遇到了这个问题,问题是由于$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命令,错误不再显示。

rxztt3cl

rxztt3cl3#

我已经修改了下面的代码。
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');
    });
}
public function down() {
    Schema::table('role_user', function (Blueprint $table) {
        $table->dropForeign(['user_id']);
        $table->dropForeign(['role_id']);
    });
}
bq9c1y66

bq9c1y664#

在Laravel 9.x中,您不需要指定单独的外键删除。

<?php

use App\Models\Question;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('answers', function (Blueprint $table) {
            $table->id();
            $table->foreignIdFor(Question::class, 'question_id');
            $table->text('correctly');
        });
    }

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

相关问题