Laravel Foreign Key Ondelete('Cascade') Not Working

Laravel foreign key onDelete('cascade') not working

Try setting when trying to create this table. This fix has worked for me.

$table->engine = 'InnoDB';

I have filed a bug under: https://github.com/laravel/framework/issues/8730

Laravel onDelete('cascade') does not work

You just write foreign key in wrong order.

Instead of:

$table->foreignId('support_category_id')->onDelete('cascade')->constrained('support_categories')->nullable();

Write:

$table->foreignId('support_category_id')->nullable()->constrained('support_categories')->onDelete('cascade');

or you can use casadeOnDelete():

$table->foreignId('support_category_id')->nullable()->constrained('support_categories')->casadeOnDelete();

Laravel adding cascade on delete to an existing table

Drop foreign key first then add it

$table->dropIndex('researach_id');
$table->foreign('research_id')
->references('id')->on('researches')
->onDelete('cascade');

Reference:
Laravel -> Database: Migrations -> Foreign Key Constraints

Cannot add foreign key constrain on delete cascade

You are trying to add the foreign key reference on the primary key of product_attributes table and wrong reference column. The correct reference to the products table

$table->unsignedBigInteger('product_id');
$table->foreign('product_id')->references('id')
->on('products')->onDelete('cascade');

The full schema of product_attributes table is

Schema::create('product_attributes', function ($table) {
$table->bigIncrements('id');
$table->string('title');
$table->string('unit')->nullable();
$table->string('type');
$table->float('price')->nullable();
$table->nestedSet();
$table->unsignedBigInteger('product_id');
$table->foreign('product_id')->references('id')
->on('products')->onDelete('cascade');
$table->timestamps();
});

onDelete('cascade') has no effect Laravel 5.6

According to your relationship structure, when you will delete a photo this will cascade down to the posts table so see if a foreign contraint failed, as it turns out yes, there is a post that refrences this photo, then that post will be deleted as well.

Consider this,

posts:
- id
- title
- body

photos:
- id
- path
- post_id - references id on posts with onDelete('cascade')

With this structure you can have photo to be deleted if a post is deleted.

Or, if you dont want to change the structure. You can consider deleting related photos on your laravel side, you can use deleting model event, to manually delete the related photos. This helps making sure that whenever a post is being deleted its related photo is deleted.

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

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

Laravel - onDelete(cascade) does not work

Maybe sqlite has foreign key support disabled (default behaviour).

See ON DELETE CASCADE in sqlite3



Related Topics



Leave a reply



Submit