提交 2c6b6e28 编写于 作者: R Rick Olson

automatically add primary key to #select_limited_ids_list order by clause for...

automatically add primary key to #select_limited_ids_list order by clause for databases that require order columns in the distinct statements (postgresql) [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5292 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 e789b26e
......@@ -1197,6 +1197,7 @@ def construct_finder_sql_for_association_limiting(options, join_dependency)
sql = "SELECT "
if is_distinct
ordered_columns = options[:order].to_s.split(',').collect! { |s| s.split.first }
options[:order] = "#{table_name}.#{primary_key}, #{options[:order]}" if options[:order] && connection.requires_order_columns_in_distinct_clause?
sql << connection.distinct("#{table_name}.#{primary_key}", ordered_columns)
else
sql << primary_key
......
......@@ -56,6 +56,12 @@ def prefetch_primary_key?(table_name = nil)
false
end
# Does this adapter require the order columns to be in the select clause
# of a DISTINCT query? This is +false+ in all adapters except postgresql.
def requires_order_columns_in_distinct_clause?
false
end
def reset_runtime #:nodoc:
rt, @runtime = @runtime, 0
rt
......
......@@ -111,6 +111,10 @@ def table_alias_length
63
end
def requires_order_columns_in_distinct_clause?
true
end
# QUOTING ==================================================
def quote(value, column = nil)
......
......@@ -1793,7 +1793,7 @@ def test_select_limited_ids_list
join_base = ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase.new(Project)
join_dep = ActiveRecord::Associations::ClassMethods::JoinDependency.new(join_base, :developers, nil)
projects = Project.send(:select_limited_ids_list, {:order => 'projects.id, developers.created_at'}, join_dep)
projects = Project.send(:select_limited_ids_list, {:order => 'developers.created_at'}, join_dep)
assert_equal "'1', '2'", projects
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册