jruby/activerecord-jdbc-adapter — Issue #172: :dependent => :nullify issue with default scope.
Author: boxofrad
Created: 2012-02-14 15:29:51+00:00
State: open

I have a model like this:

```ruby
class Thing
belongs_to :stuff
default_scope order('foo DESC, bar DESC')
end
```

And another like this:

```ruby
class Stuff
has_many :things,
:dependent => :nullify
end
```

And when I destroy the second model `@stuff.destroy` I get the following exception:

```
ActiveRecord::JDBCError: The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.: UPDATE [things] SET [stuff_id] = NULL WHERE [things].[id] IN (SELECT [things].[id] FROM [things] WHERE [things].[stuff_id] = 4 AND [things].[id] IN (73, 72) ORDER BY foo DESC, bar DESC)
```

Aside from not working this SQL seems needlessly complex and inefficient..

It does 3 queries (one to get the ID of the things which we dont really need to know) then it does subquery to get the IDs we've already got!!

Why not

```sql
UPDATE [things]
SET [things].[stuff_id] = NULL
WHERE [things].[stuff_id] = {INJECT stuff_id}
```