Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

belongs_to :dependent => :destroy should destroy self before assocation

#1079

: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.

Reported by Ben VandenBos · September 19th, 2008 @ 11:36 PM

State: resolved
Milestone: 2.x
Assigned to: Frederick Cheung Frederick Cheung
Importance: none

Activity

  1. Pratik
    Pratik
    • 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.

    January 5th, 2009 @ 06:57 PM

  2. Frederick Cheung
    Frederick Cheung

    Seems to resolve the issue for me. Can't think of an obviously bad side effect

    January 5th, 2009 @ 07:34 PM

  3. Repository
    Repository
    • State changed from new to resolved

    (from [0e92f67073079fe3d34acd141099cdad28b0ee00]) Make belongs_to :dependent => :destroy destroy self before associated object [#1079 belongs_to :dependent => :destroy should destroy self before assocation 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...

    January 16th, 2009 @ 05:40 PM