'Laravel how to modify onDelete('cascade') into no action in migration?
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()
?
Solution 1:[1]
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
Solution 2:[2]
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'));
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Geziel Carvalho |