未验证 提交 c9981ae6 编写于 作者: R Ryuta Kamizono 提交者: GitHub

Merge pull request #35850 from kamipo/fix_count_all_with_eager_loading_and_select_and_order

Fix `count(:all)` with eager loading and explicit select and order
......@@ -129,11 +129,12 @@ def calculate(operation, column_name)
relation = apply_join_dependency
if operation.to_s.downcase == "count"
relation.distinct!
# PostgreSQL: ORDER BY expressions must appear in SELECT list when using DISTINCT
if (column_name == :all || column_name.nil?) && select_values.empty?
relation.order_values = []
unless distinct_value || distinct_select?(column_name || select_for_count)
relation.distinct!
relation.select_values = [ klass.primary_key || table[Arel.star] ]
end
# PostgreSQL: ORDER BY expressions must appear in SELECT list when using DISTINCT
relation.order_values = []
end
relation.calculate(operation, column_name)
......
......@@ -243,6 +243,12 @@ def test_count_with_eager_loading_and_custom_order
assert_queries(1) { assert_equal 11, posts.count(:all) }
end
def test_count_with_eager_loading_and_custom_select_and_order
posts = Post.includes(:comments).order("comments.id").select(:type)
assert_queries(1) { assert_equal 11, posts.count }
assert_queries(1) { assert_equal 11, posts.count(:all) }
end
def test_count_with_eager_loading_and_custom_order_and_distinct
posts = Post.includes(:comments).order("comments.id").distinct
assert_queries(1) { assert_equal 11, posts.count }
......
......@@ -985,6 +985,12 @@ def test_size_with_eager_loading_and_custom_order
assert_queries(1) { assert_equal 11, posts.load.size }
end
def test_size_with_eager_loading_and_custom_select_and_order
posts = Post.includes(:comments).order("comments.id").select(:type)
assert_queries(1) { assert_equal 11, posts.size }
assert_queries(1) { assert_equal 11, posts.load.size }
end
def test_size_with_eager_loading_and_custom_order_and_distinct
posts = Post.includes(:comments).order("comments.id").distinct
assert_queries(1) { assert_equal 11, posts.size }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册