Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to rely on foreign key cascading?

The lead developer on a project I'm involved in says it's bad practice to rely on cascades to delete related rows.

I don't see how this is bad, but I would like to know your thoughts on if/why it is.

like image 332
Keyo Avatar asked Feb 05 '10 03:02

Keyo


People also ask

Why cascade delete is bad?

Why sql server cascade delete is bad? sql server cascade delete should not cause an unexpected loss of data. If a delete requires related records to be deleted, and the user needs to know that those records are going to go away, then cascading deletes should not be used.

Is Cascade delete bad practice?

a) do you consider "on delete cascade" to be a bad practice ? yes, I would. It is a side effect - an automagic side effect.

Is Cascade a foreign key constraint?

What is a foreign key with Cascade DELETE in SQL Server? A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete in SQL Server.


Video Answer


1 Answers

I'll preface this by saying that I rarely delete rows period. Generally most data you want to keep. You simply mark it as deleted so it won't be shown to users (ie to them it appears deleted). Of course it depends on the data and for some things (eg shopping cart contents) actually deleting the records when the user empties his or her cart is fine.

I can only assume that the issue here is you may unintentionally delete records you don't actually want to delete. Referential integrity should prevent this however. So I can't really see a reason against this other than the case for being explicit.

like image 154
cletus Avatar answered Sep 24 '22 08:09

cletus