php 将“ON DELETE CASCADE”添加到 Laravel 中的现有列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26820788/
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-08-25 23:04:22  来源:igfitidea点击:

Add "ON DELETE CASCADE" to existing column in Laravel

phplaravellaravel-4database-migration

提问by Farid Movsumov

I have user_id fk column in my table

我的表中有 user_id fk 列

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

I should add on cascade deletefeature to this existing column. How can I do this?

我应该向这个现有的列添加级联删除功能。我怎样才能做到这一点?

回答by Farid Movsumov

Drop foreign key first. Thanks to Razorfor this tip

先删除外键。感谢Razor的提示

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

回答by Mark Baker

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

回答by Razor

Laravel schema builder can't modify columns at the current state, so you will use raw queries. You will have to drop and recreate the constraint:

Laravel 模式构建器无法修改当前状态下的列,因此您将使用原始查询。您将不得不删除并重新创建约束

PostgreSQL

PostgreSQL

function up()
{
    DB::statement('alter table answers drop constraint answers_user_id_foreign,
                   add constraint answers_user_id_foreign
                   foreign key (user_id)
                   references users(id)
                   on delete cascade;'
    );
}
function down()
{
    DB::statement('alter table answers drop constraint answers_user_id_foreign,
                   add constraint answers_user_id_foreign
                   foreign key (user_id)
                   references users(id);'
    );
}

MySQL

MySQL

function up()
{
    DB::statement('alter table answers drop FOREIGN KEY answers_user_id_foreign;');
    DB::statement('alter table answers add constraint answers_user_id_foreign
                   foreign key (user_id)
                   references users(id)
                   on delete cascade;'
    );
}
function down()
{
    DB::statement('alter table answers drop FOREIGN KEY answers_user_id_foreign;');
    DB::statement('alter table answers add constraint answers_user_id_foreign
                   foreign key (user_id)
                   references users(id);'
    );
}

回答by shalonteoh

In my case, i'll need to put the col name in an array else that will be an error.

就我而言,我需要将 col 名称放在一个数组中,否则会出错。

Schema::table('transactions', function (Blueprint $table) {
    $table->dropForeign(['transactions_order_id_foreign']);
    $table->foreign('order_id')
        ->references('id')->on('orders')
        ->onDelete('cascade')
        ->change();
});

mysql 5.7 ver

mysql 5.7 版本

回答by JohnWolf

Thanks for question answer. Help me get to this working code in L5.1 :

感谢您的提问。帮助我在 L5.1 中获取此工作代码:

public function up()
{
    Schema::table('transactions', function (Blueprint $table) {
        $table->dropForeign('transactions_order_id_foreign');
        $table->foreign('order_id')
            ->references('id')->on('orders')
            ->onDelete('cascade')
            ->change();
    });

    Schema::table('orders', function (Blueprint $table) {
        $table->dropForeign('orders_user_id_foreign');
        $table->foreign('user_id')
            ->references('id')->on('users')
            ->onDelete('cascade')
            ->change();
    });
}

回答by Detroit Charan

Use the unsigned function to user_idin the present migration:

user_id在当前的迁移中使用 unsigned 函数:

$table->interger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('table_name')->onDelete('cascade');

回答by Inda5th

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

I am assuming you used Illuminate\Database\Schema\Blueprint::primary()to create users.id. If that is the case, then users.idwill be unsigned. Therefore your foreign key column user_idmust also be unsigned.

我假设你曾经Illuminate\Database\Schema\Blueprint::primary()创建users.id. 如果是这样,那么users.id将是未签名的。因此,您的外键列user_id也必须是未签名的。