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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39072220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:21:10  来源:igfitidea点击:

How i drop foreign and primary key of table in migration?

phplaravellaravel-5laravel-5.5

提问by Gurpreet Singh

How I drop multiple foreign key and primary keys in migration file.

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

Bellow is my migration file code.

波纹管是我的迁移文件代码。

Migration File

迁移文件

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 ?
}

回答by jedrzej.kurylo

Blueprintclass offers dropForeignand dropPrimarymethods that allow you to remove foreign key constraints and primary key.

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

The following should do the trick:

以下应该可以解决问题:

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();
    });
}