Laravel how to modify onDelete('cascade') into no action in migration?

Viewed 3912

I have a users table which has many debitur. I already set the user_id in debitur with onDelete('cascade').

Schema::table('debitur', function ($table) {
    $table->index('user_id');
    $table
    ->foreign('user_id')
    ->references('id')
    ->on('users')
    ->onDelete('cascade')
    ->onUpdate('cascade');

Now I want to modify the onDelete('cascade') into no action, but the docs say for a no action I shouldn't have set onDelete().

So how do I modify it? should I drop the foreign then set it again, or can I just re-run schema::table omitting the onDelete()?

2 Answers

Create a new migration:

Schema::table('debitur', function ($table) {
    $table->dropForeign('user_id');
    $table
    ->foreign('user_id')
    ->references('id')
    ->on('users')
    ->onUpdate('cascade');
}

Source: Laravel Foreign Key Constraints

As I can see, you've already set onDelete('cascade') and now wanna change the action to NO ACTION.

Well, you've got to drop your previous foreign key and create another.

Here's my migration code.

public function up()
{
    Schema::table('tablename', function ($table) {
        $table->dropForeign('tablename_id_foreign');
        $table->foreign('othertable_id')
            ->references('id')
            ->on('othertables')
            ->onDelete(\DB::raw('NO ACTION'));
    });
}
Related