DEV Community

Different type of action to take on delete in Laravel database migration

Different type of action to take on delete in Laravel database migration.

When defining a foreign key in a Laravel migration, the onDelete method determines what happens to the child records when the parent record is deleted. This allows you to manage data integrity at the database level.

Here are the different types of onDelete actions you can use:

cascade

The CASCADE action will automatically delete all child records when their parent record is deleted. This is useful for "has many" relationships where the child records are meaningless without the parent.

  • Example: If a user is deleted, all of their posts are also deleted.

Migration code:

$table->foreignId('user_id')->constrained()->cascadeOnDelete();
Enter fullscreen mode Exit fullscreen mode

Migration code:


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

set null

The SET NULL action will set the foreign key on all child records to null when the parent record is deleted. This is useful for optional relationships where the child record can still exist without the parent.

  • Example: When a user is deleted, the user_id on their posts is set to null, allowing the posts to remain without an author. Note: Requirement: The foreign key column must be set as nullable() in the migration.

Migration code:


$table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
Enter fullscreen mode Exit fullscreen mode

Migration code:

$table->unsignedBigInteger('user_id')->nullable();
$table->foreign('user_id')
      ->references('id')->on('users')
      ->onDelete('set null');
Enter fullscreen mode Exit fullscreen mode

restrict

The RESTRICT action prevents the parent record from being deleted as long as there are still child records referencing it. This is the default behavior if no onDelete action is specified.

  • Example: If a category has associated products, you cannot delete the category until all products have been re-assigned or deleted.

Migration code: No onDelete method is called.


$table->foreignId('category_id')->constrained();

# OR

$table->foreign('category_id')
      ->references('id')->on('categories')
      ->onDelete('restrict');

Enter fullscreen mode Exit fullscreen mode

no action

The NO ACTION action is similar to RESTRICT. It defers the integrity check until the end of the transaction, but in practice, most database engines implement it the same way as RESTRICT.

  • Migration code: You can explicitly add this, though RESTRICT or no action is sufficient.

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

Enter fullscreen mode Exit fullscreen mode

Choosing the right onDelete action

Scenario Action Example
Delete all child records cascade Deleting a user should also delete all of their comments.
Keep child records and unset the foreign key set null Deleting an author should keep their articles, with the author field becoming blank.
Prevent deletion until child records are removed restrict You cannot delete a product category if products are still assigned to it.

Note: This is also similar to on update.

Have a nice day!

Top comments (0)