gpt4 book ai didi

laravel - 在 Laravel 迁移中删除外键

转载 作者:行者123 更新时间:2023-12-04 10:23:08 25 4
gpt4 key购买 nike

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

php artisan migrate:rollback

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

[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]: Syntax error or access violation: 1091 Can't DROP 'role_user_user_id_foreign'; check that column/key exists

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



下面我展示了我的迁移类(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');

});
}
}

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

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 中,它允许将列名放入一个数组中,然后它会自行解析。我试图找到随附的文档,但他们似乎已将其排除在外。

在您的更新迁移中,试试这个:

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

关于laravel - 在 Laravel 迁移中删除外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44188450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com