I am trying to decide how best to configure (if at all) the external key constraints for my rails application. I have a Response model that belongs_to a Prompt . I would like to use :dependent => :destroy to call destroy on each Response that belongs to the remote Prompt , and I am trying to decide which delete restriction I should place on my foreign key.
In short, I need advice on how I can best use both the destroy method for dependent objects and foreign key constraints to ensure that the cool does not accumulate and does not reflect the logical structure of the stored data. A few previous questions, such as Should I use ON DELETE CASCADE ,: dependent =>: destroy or both? and Rails: removing the cascade against dependent destruction asked what was better, but they donβt talk very much about how the two options interact and in what order they work or seem vague in this matter.
As I see it, the considerations seem to break into several parts:
- Does the call
:dependent => :destroy first on dependent objects before removing the parent from the database, so destruction will be called on these objects, even if I use cascading deletion? Does :dependent => :destroy remove :dependent => :destroy dependent objects from the database before (or in the transaction) with the parent removed from the database? In other words, if I set up a cascade for invalidation, does the database end up emptying references to child objects before they are deleted?
Are the original options for destruction and binding deleted :dependent => :destroy , wrapped in a transaction, or, unfortunately, during short-term database failures, if I do not install cascading deletion?
/ li>- Finally, will
:dependent => :destroy ensure that the parent will be deleted from the database if I use the constraint as a foreign key parameter on_delete?
ruby-on-rails foreign-keys cascade
Peter Gerdes
source share