This project is archived and is in readonly mode.

#3086 ✓resolved
Chris Hapgood

Association :dependent => delete_all + :foreign_key can result in data corruption

Reported by Chris Hapgood | August 21st, 2009 @ 04:34 PM | in 2.3.6

"Code Red, Mission Critical, The World is Coming to an End"

Well, maybe the world isn't coming to an end, but zombies are walking around in my database. If I define a has_many association with both the :foreign_key option and :dependent => :delete_all, deleting the object does not, in fact, delete any dependent records.

Given an association defined as follows:

class User
  has_many :permissions, :primary_key => 'auth_token', :foreign_key => 'token', :dependent => :delete_all
end

Expected Behavior When I destroy an instance of User
Then Permission instances are deleted where user.auth_token == permission.token

*Observed Behavior* When I destroy an instance of User
Then Permission instances are deleted where user.id == permissions.token

In addition to NOT deleting the expected records, this bug runs the real risk of deleting innocent unrelated records.

The bug has been observed in 2.3.1 and 2.3.3.

Comments and changes to this ticket

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