Fixed #32546 -- Avoided Meta.ordering columns in GROUP BY clauses.

Follow up to 0ddb4ebf7b.
This commit is contained in:
Yuri Konotopov 2021-03-13 23:25:40 +04:00 committed by Mariusz Felisiak
parent bc04941bf8
commit 330bc402a8
2 changed files with 16 additions and 6 deletions

View File

@ -125,11 +125,12 @@ class SQLCompiler:
cols = expr.get_group_by_cols()
for col in cols:
expressions.append(col)
for expr, (sql, params, is_ref) in order_by:
# Skip References to the select clause, as all expressions in the
# select clause are already part of the group by.
if not is_ref:
expressions.extend(expr.get_group_by_cols())
if not self._meta_ordering:
for expr, (sql, params, is_ref) in order_by:
# Skip references to the SELECT clause, as all expressions in
# the SELECT clause are already part of the GROUP BY.
if not is_ref:
expressions.extend(expr.get_group_by_cols())
having_group_by = self.having.get_group_by_cols() if self.having else ()
for expr in having_group_by:
expressions.append(expr)

View File

@ -2,7 +2,7 @@ from datetime import datetime
from operator import attrgetter
from django.db.models import (
CharField, DateTimeField, F, Max, OuterRef, Subquery, Value,
CharField, Count, DateTimeField, F, Max, OuterRef, Subquery, Value,
)
from django.db.models.functions import Upper
from django.test import TestCase
@ -484,3 +484,12 @@ class OrderingTests(TestCase):
ca4 = ChildArticle.objects.create(headline='h1', pub_date=datetime(2005, 7, 28))
articles = ChildArticle.objects.order_by('article_ptr')
self.assertSequenceEqual(articles, [ca4, ca2, ca1, ca3])
def test_default_ordering_does_not_affect_group_by(self):
Article.objects.exclude(headline='Article 4').update(author=self.author_1)
Article.objects.filter(headline='Article 4').update(author=self.author_2)
articles = Article.objects.values('author').annotate(count=Count('author'))
self.assertCountEqual(articles, [
{'author': self.author_1.pk, 'count': 3},
{'author': self.author_2.pk, 'count': 1},
])