gpt4 book ai didi

mysql - 如何在 Laravel 中重命名外键

转载 作者:太空宇宙 更新时间:2023-11-03 11:24:52 25 4
gpt4 key购买 nike

我想在 Laravel 中重命名外键。

我是这样创建的:

Schema::create('holidays', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('account_id')->unsigned();
$table->date('holiday_date');
});

if (Schema::hasTable('accounts')) {
Schema::table(
'holidays',
function (Blueprint $table) {
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
}
);
}

现在,我想将 account_id 更改为 engagement_id。如何做到这一点?

最佳答案

应该是这样的:

注意:重命名foreign之前,必须先删除旧的foreign并分配新的

class RenameColumn extends Migration
{

public function up()
{
Schema::table('holidays', function(Blueprint $table) {
$table->dropForeign('holidays_account_id_foreign');
$table->renameColumn('account_id', 'engagement_id');

$table->foreign('engagement_id')->references('id')->on('accounts')->onDelete('cascade');
});
}

public function down()
{
Schema::table('holidays', function(Blueprint $table) {
$table->dropForeign('holidays_engagement_id_foreign');
$table->renameColumn('account_id', 'engagement_id');

$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
});
}
}

关于mysql - 如何在 Laravel 中重命名外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55260939/

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