This project is archived and is in readonly mode.
belongs_to :dependent => :destroy should destroy self before assocation
Reported by Ben VandenBos | September 19th, 2008 @ 11:36 PM | in 2.x
:belongs_to :dependent => :destroy leaves the model open to foreign key constraint failure upon deletion. This seems be due to it using before_destroy as opposed to after_destroy. It can't destroy the associated model before it deletes itself because of the foreign key. I believe the fix is to simply use after_destroy (patch attached) to destroy :belongs_to associations after it's destroyed itself.
Example:
class Person < ActiveRecord::Base
belongs_to :person_address, :dependent => :destroy
end
class PersonAddress < ActiveRecord::Base
has_one :person
end
...and a foreign key on person_address_id on the person table to id on the person_address table.
Comments and changes to this ticket
-
Pratik January 5th, 2009 @ 06:57 PM
- Tag changed from activerecord, bug to activerecord, bug, patch
- Assigned user set to Frederick Cheung
Seems reasonable to me. Assigning to Fred to make sure I'm not missing anything. Will apply after his +1.
-
Frederick Cheung January 5th, 2009 @ 07:34 PM
Seems to resolve the issue for me. Can't think of an obviously bad side effect
-
Repository January 16th, 2009 @ 05:40 PM
- State changed from new to resolved
(from [0e92f67073079fe3d34acd141099cdad28b0ee00]) Make belongs_to :dependent => :destroy destroy self before associated object [#1079 state:resolved]
If foreign key constraints are in place then deleteing the associated object first will cause a foreign key violation
Signed-off-by: Frederick Cheung frederick.cheung@gmail.com http://github.com/rails/rails/co...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Tags
Referenced by
- 1079 belongs_to :dependent => :destroy should destroy self before assocation (from [0e92f67073079fe3d34acd141099cdad28b0ee00]) Make be...