diff options
| author | Yuri Konotopov <ykonotopov@gnome.org> | 2021-03-13 23:25:40 +0400 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-03-16 07:58:38 +0100 |
| commit | 330bc402a8d2d8f23cf2e07d9dabf333003677d3 (patch) | |
| tree | 681b047a1e4279a16c0e5eedf6b3401b98357ef5 | |
| parent | bc04941bf811d1ea2c79fb7fc20457ed2c7e3410 (diff) | |
Fixed #32546 -- Avoided Meta.ordering columns in GROUP BY clauses.
Follow up to 0ddb4ebf7bfcc4730c80a772dd146a49ef6895f6.
| -rw-r--r-- | django/db/models/sql/compiler.py | 11 | ||||
| -rw-r--r-- | tests/ordering/tests.py | 11 |
2 files changed, 16 insertions, 6 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 850734709d..f02199d97c 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -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) diff --git a/tests/ordering/tests.py b/tests/ordering/tests.py index c8e9c98e43..604c72c1a2 100644 --- a/tests/ordering/tests.py +++ b/tests/ordering/tests.py @@ -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}, + ]) |
