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.

This project is archived and is in readonly mode.

lock_version, counter cache and dependent as destroy

#3073

This is rather minor issue.

Example

class Parent < ActiveRecord::Base
  has_many children, :dependent => :destroy
  # this model has lock_version and children_count fields
end


class Child < ActiveRecord::Base belongs_to :parent, :counter_cache => true end

The if the parent has child records then "parent.destroy" doesn't work. "parent.destroy" calls destroy for all children objects, which in turn update the parent children_count together with lock_version fields. In this way the parent becomes "stalled" and actual SQL delete on the parent fails. Because of "stalled object" exception whole transaction is rolled back. "parent.children.clear; parent.destroy" behave the same.

A workaround is to remove the lock_version field from the parent model.

Reported by Greg Dan · August 19th, 2009 @ 11:16 AM

State: stale
Milestone: none
Assigned to: Rizwan Reza Rizwan Reza
Importance: none

Activity

  1. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activerecord to activerecord, bugmash

    September 25th, 2009 @ 12:50 PM

  2. Rizwan Reza
    Rizwan Reza
    • State changed from new to open
    • Tag changed from activerecord, bugmash to activerecord, associations

    Can you provide a failing test for this?

    January 21st, 2010 @ 07:11 AM

  3. masone
    masone
    • Importance changed from to

    I tried to reproduce this one. For me it's not reproducible.

    Rails 2.3.8, Ruby 1.8.7 p299, OSX
    Rails 3.0.0.beta4, Ruby 1.8.7 p299, OSX
    Manually tested with mysql and sqlite

    Did I get something wrong or is the problem fixed by now?
    I used the controllers as described by Dan and the following migrations:

    class CreateParents < ActiveRecord::Migration
      def self.up
        create_table :parents do |t|
    
          t.integer :children_count, :default => 0
          t.integer :lock_version, :default => 0
          t.text :body
          t.timestamps
        end
      end
    
      def self.down
        drop_table :parents
      end
    end
    
    class CreateChildren < ActiveRecord::Migration
      def self.up
        create_table :children do |t|
          t.text :body
          t.integer :parent_id
          t.timestamps
        end
      end
    
      def self.down
        drop_table :children
      end
    end
    

    Console:

    ruby-1.8.7-p299 > p = Parent.create(:body => 'foo')
     => #<Parent id: 1, children_count: 0, lock_version: 0, body: "foo", created_at: "2010-07-16 13:57:59", updated_at: "2010-07-16 13:57:59"> 
    ruby-1.8.7-p299 > Child.create(:body => 'foo', :parent => p)
     => #<Child id: 1, body: "foo", parent_id: 1, created_at: "2010-07-16 13:57:59", updated_at: "2010-07-16 13:57:59"> 
    ruby-1.8.7-p299 > Child.create(:body => 'bar', :parent => p)
     => #<Child id: 2, body: "bar", parent_id: 1, created_at: "2010-07-16 13:57:59", updated_at: "2010-07-16 13:57:59"> 
    ruby-1.8.7-p299 > p.reload
     => #<Parent id: 1, children_count: 2, lock_version: 2, body: "foo", created_at: "2010-07-16 13:57:59", updated_at: "2010-07-16 13:57:59"> 
    ruby-1.8.7-p299 > Parent.first.destroy
     => #<Parent id: 1, children_count: 2, lock_version: 2, body: "foo", created_at: "2010-07-16 13:57:59", updated_at: "2010-07-16 13:57:59"> 


    ruby-1.8.7-p299 > Parent.all => [] ruby-1.8.7-p299 > Child.all => []

    July 16th, 2010 @ 03:02 PM

  4. Jacob Burkhart
    Jacob Burkhart

    I am currently seeing this in our project. With the additional constraint that the "class Parent" (as in example of original ticket filing) uses single table inheritance.

    Attaching patch with fix and test. (one for master, one for the next 2.3.x release)

    July 28th, 2010 @ 08:50 PM

  5. Jacob Burkhart
    Jacob Burkhart
    • Assigned user set to Rizwan Reza

    July 28th, 2010 @ 08:54 PM

  6. Santiago Pastorino
    Santiago Pastorino

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:34 PM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:34 PM