Dropping foreign keys in Laravel migration

后端 未结 3 2478
花落未央
花落未央 2021-02-20 14:58

I have a problem with dropping some foreign keys from my Laravel application. The problem is when I am trying to rollback the migration:

php artisan migrate:roll         


        
3条回答
  •  佛祖请我去吃肉
    2021-02-20 15:56

    I have modified your code below.

    Add the onDelete() and onUpdate() to your code.

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

提交回复
热议问题