Alex Rivera | Logout

Is it bad to rely on foreign key cascading?

Asked 2010-02-05T03:10:45.520
37

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.

Edit
Report

1 Answer

30

I would say that you follow the principle of least surprise.

Cascading deletes should not cause 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. Instead, the user should be required to explicitly delete the related records, or be provided a notification.

On the other hand, if the table relates to another table that is temporary in nature, or that contains records that will never be needed once the parent entity is gone, then cascading deletes may be OK.

That said, I prefer to state my intentions explicitly by deleting the related records in code, rather than relying on cascading deletes. In fact, I've never actually used a cascading delete to implicitly delete related records. Also, I use soft deletion a lot, as described by cletus.

answered 2010-02-05T03:18:18.067

Your Answer