rails/rails — pull request #6523 by Empact (2012-05-28 20:50:26+00:00)
Title: Failing test for #exists? SELECT DISTINCT DISTINCT bug

Looking for input on how to handle this. Apparently the combination of a :uniq => true association and the #distinct call in construct_limited_ids_condition combine to create invalid SQL.

Bug is present on 3-2-stable as well.

Fails with:

ActiveRecord::StatementInvalid: SQLite3::SQLException: near "DISTINCT": syntax error: SELECT DISTINCT DISTINCT "posts".id FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" AND "comments"."type" IN ('SpecialComment', 'SubSpecialComment') INNER JOIN "categorizations" ON "posts"."id" = "categorizations"."post_id" WHERE "categorizations"."author_id" = ? LIMIT 0
/Users/ben/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in `initialize'
/Users/ben/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in `new'
/Users/ben/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in `prepare'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:288:in `block in exec_query'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:288:in `block in log'
/Users/ben/Projects/OpenSource/rails/activesupport/lib/active_support/notifications/instrumenter.rb:18:in `instrument'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:283:in `log'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:277:in `exec_query'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:495:in `select'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/querying.rb:40:in `block in find_by_sql'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/explain.rb:37:in `logging_query_plan'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/querying.rb:39:in `find_by_sql'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/relation.rb:174:in `exec_queries'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/relation.rb:164:in `block in to_a'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/explain.rb:37:in `logging_query_plan'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/relation.rb:163:in `to_a'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/relation/delegation.rb:6:in `collect'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/relation/finder_methods.rb:241:in `construct_limited_ids_condition'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/relation/finder_methods.rb:226:in `apply_join_dependency'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/relation/finder_methods.rb:215:in `construct_relation_for_association_find'
/Users/ben/Projects/OpenSource/rails/activerecord/lib/active_record/relation/finder_methods.rb:177:in `exists?'
test/cases/finder_test.rb:68:in `test_exists_with_distinct_association_includes_and_limit'