aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
diff options
context:
space:
mode:
authorBen Woosley <ben.woosley@gmail.com>2012-05-28 12:23:37 -0700
committerBen Woosley <ben.woosley@gmail.com>2013-05-10 16:13:46 +0200
commit15d6e4dce7126fe24bce5cdb91d2ffee68648420 (patch)
treedd482d3d61b0b68aba9a085a8bd7929eda45feae /activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
parent0593c00dfbdab221116e49d3b0a7c57605160fe2 (diff)
downloadrails-15d6e4dce7126fe24bce5cdb91d2ffee68648420.tar.gz
rails-15d6e4dce7126fe24bce5cdb91d2ffee68648420.tar.bz2
rails-15d6e4dce7126fe24bce5cdb91d2ffee68648420.zip
Fix that #exists? can produce invalid SQL: "SELECT DISTINCT DISTINCT"
The combination of a :uniq => true association and the #distinct call in #construct_limited_ids_condition combine to create invalid SQL, because we're explicitly selecting DISTINCT, and also sending #distinct on to AREL, via the relation#distinct_value. Rather than build a select distinct clause in #construct_limited_ids_condition, I set #distinct! and pass just the columns into the select statement. This requires introducing a #columns_for_distinct method to return the select columns but not the statement itself.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb9
1 files changed, 2 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
index 98916b06a5..8feee23df0 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -467,14 +467,9 @@ module ActiveRecord
end
end
- # Returns a SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
- #
# PostgreSQL requires the ORDER BY columns in the select list for distinct queries, and
# requires that the ORDER BY include the distinct column.
- #
- # distinct("posts.id", ["posts.created_at desc"])
- # # => "DISTINCT posts.id, posts.created_at AS alias_0"
- def distinct(columns, orders) #:nodoc:
+ def columns_for_distinct(columns, orders) #:nodoc:
order_columns = orders.map{ |s|
# Convert Arel node to string
s = s.to_sql unless s.is_a?(String)
@@ -482,7 +477,7 @@ module ActiveRecord
s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '')
}.reject(&:blank?).map.with_index { |column, i| "#{column} AS alias_#{i}" }
- [super].concat(order_columns).join(', ')
+ [super, *order_columns].join(', ')
end
end
end