rails/rails — Issue #4495: delete_all dependency fails with custom primary & foreign keys
Author: JonMidhir
Created: 2012-01-17 11:46:16+00:00
State: open

Seems to be an issue with the SQL generated to delete all children when trying to delete the parent object when a has_many association uses custom foreign and primary keys. Causes an ActiveRecord::StatementInvalid error and deletion of the parent fails.

For example:

```
class Book < ActiveRecord::Base
has_many :pages, :foreign_key => 'book_bid', :primary_key => 'bid', :dependent => :delete_all
end
```

```
class Page < ActiveRecord::Base
belongs_to :book, :foreign_key => "book_bid", :primary_key => "bid"
end
```

I've made a rough example app to demonstrate the bug here: https://github.com/JonMidhir/Rails-Dependent-destroy_all-Bug-Example

We're in crunch time at the minute but when we're out of it I can probably fix this if it really is a bug.