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

Merge pull request #39297 from kamipo/fix_group_by_order_and_limit_offset

Fix group by count with eager loading + order + limit/offset
......@@ -136,7 +136,7 @@ def calculate(operation, column_name)
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 = []
relation.order_values = [] if group_values.empty?
end
relation.calculate(operation, column_name)
......
......@@ -851,20 +851,20 @@ def test_group_by_with_order_by_virtual_count_attribute
end if current_adapter?(:PostgreSQLAdapter)
def test_group_by_with_limit
expected = { "Post" => 8, "SpecialPost" => 1 }
actual = Post.includes(:comments).group(:type).order(:type).limit(2).count("comments.id")
expected = { "StiPost" => 2, "SpecialPost" => 1 }
actual = Post.includes(:comments).group(:type).order(type: :desc).limit(2).count("comments.id")
assert_equal expected, actual
end
def test_group_by_with_offset
expected = { "SpecialPost" => 1, "StiPost" => 2 }
actual = Post.includes(:comments).group(:type).order(:type).offset(1).count("comments.id")
expected = { "SpecialPost" => 1, "Post" => 8 }
actual = Post.includes(:comments).group(:type).order(type: :desc).offset(1).count("comments.id")
assert_equal expected, actual
end
def test_group_by_with_limit_and_offset
expected = { "SpecialPost" => 1 }
actual = Post.includes(:comments).group(:type).order(:type).offset(1).limit(1).count("comments.id")
actual = Post.includes(:comments).group(:type).order(type: :desc).offset(1).limit(1).count("comments.id")
assert_equal expected, actual
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册