我如何在迁移中删除表的外键和主键?

问题描述 投票:1回答:1

我如何在迁移文件中删除多个外键和主键。

Bellow是我的迁移文件代码。

迁移文件

public function up()
{
    Schema::create('role_user', function(Blueprint $table){
        $table->integer('role_id')->unsigned();
        $table->integer('user_id')->unsigned();

        $table->foreign('role_id')
                ->references('id')
                ->on('roles')
                ->onDelete('cascade');

        $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

        $table->primary(['role_id', 'user_id']);
    });
}
public function down()
{
    Schema::drop('role_user');
    //how drop foreign and primary key here ?
}
php laravel laravel-5 laravel-5.5
1个回答
10
投票

Blueprint类提供了dropForeign和dropPrimary方法,允许您删除外键约束和主键。

以下应该做的伎俩:

public function down()
{
    Schema::table('role_user', function (Blueprint $table) {
        $table->dropForeign('role_user_role_id_foreign');
        $table->dropForeign('role_user_user_id_foreign');
        $table->dropPrimary();
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.