Skip to content


Fix FinderMethods#exists? sql generation to make picky sql servers happy #1139

wants to merge 6 commits into from

4 participants


Minimal change to query generation of FinderMethods#exists? that makes SQLServer and others happy that do not like columns without an alias.

Without this patch MS-SQLServer doesn't like the generated SQL and barks for FinderMethods#exists?, ie:

ActiveRecord::StatementInvalid: ActiveRecord::JDBCError: No column was specified for column 2 of 't'.: SELECT t.* FROM (SELECT ROW_NUMBER() OVER(ORDER BY AS _row_num, 1 FROM huhu WHERE huhu.[id] = 83287) AS t WHERE t._row_num BETWEEN 1 AND 1


A test case has been added.


@iaddict out of curiosity, since exists? is extensively tested, does the existent tests break against SQL Server?

Ruby on Rails member

This can't be merged automatically.

If you still have this issue, please rebase it and I'll glad to put this is in discussion with the Core Team.

Ruby on Rails member

This seems to be recurrent, I've already closed 3 issues explaining the same problem, linked above. Unfortunately this pull request does not apply anymore, as @rafaelfranca pointed, so if you can bring it up-to-date, we can go ahead with it. Thanks

@rafaelfranca rafaelfranca added a commit that closed this pull request
@rafaelfranca rafaelfranca Add test to column alias in `exists?` SQL.
This behavior was added in dd286a4

Closes #1139.

Fixes #2553, #1141, #1623 and #2062.
@frodsan frodsan pushed a commit that referenced this pull request
@rafaelfranca rafaelfranca Add test to column alias in `exists?` SQL.
This behavior was added in be4ecdc.

Closes #1139.

Fixes #2553, #1141, #1623 and #2062.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 19, 2011
  1. @iaddict

    Minimal change to query generation of exists? that makes SQLServer an…

    iaddict committed
    …d others happy that do not work without a column alias.
Commits on Jul 18, 2011
  1. @iaddict
Commits on Jul 25, 2011
  1. @iaddict
Commits on Aug 17, 2011
  1. @iaddict
  2. @iaddict
  3. @iaddict
This page is out of date. Refresh to see the latest.
2 activerecord/lib/active_record/relation/finder_methods.rb
@@ -185,7 +185,7 @@ def exists?(id = nil)
join_dependency = construct_join_dependency_for_association_find
relation = construct_relation_for_association_find(join_dependency)
- relation = relation.except(:select).select("1").limit(1)
+ relation = relation.except(:select).select("1 AS _one").limit(1)
case id
when Array, Hash
7 activerecord/test/cases/log_subscriber_test.rb
@@ -71,6 +71,13 @@ def test_exists_query_logging
assert_match(/SELECT .*?FROM .?developers.?/i, @logger.logged(:debug).last)
+ def test_exists_query_alias
+ Developer.exists? 1
+ wait
+ assert_equal 1, @logger.logged(:debug).size
+ assert_match(/SELECT .*?1 AS _one.*? FROM .?developers.?/i, @logger.logged(:debug).last)
+ end
def test_cached_queries
ActiveRecord::Base.cache do
Something went wrong with that request. Please try again.