This project is archived and is in readonly mode.

#3073 ✓stale
Greg Dan

lock_version, counter cache and dependent as destroy

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

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.

Comments and changes to this ticket

  • CancelProfileIsBroken

    CancelProfileIsBroken September 25th, 2009 @ 12:50 PM

    • Tag changed from activerecord to activerecord, bugmash
  • Rizwan Reza

    Rizwan Reza January 21st, 2010 @ 07:11 AM

    • State changed from “new” to “open”
    • Tag changed from activerecord, bugmash to activerecord, associations

    Can you provide a failing test for this?

  • masone

    masone July 16th, 2010 @ 03:02 PM

    • 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 => []
  • Jacob Burkhart

    Jacob Burkhart July 28th, 2010 @ 08:50 PM

    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)

  • Jacob Burkhart

    Jacob Burkhart July 28th, 2010 @ 08:54 PM

    • Assigned user set to “Rizwan Reza”
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:34 PM

    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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:34 PM

    • State changed from “open” to “stale”

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>

Pages