mirror of https://github.com/django/django.git
Fixed #34464 -- Fixed queryset aggregation over group by reference.
Regression in 59bea9efd2
.
Refs #28477.
Thanks Ian Cubitt for the report.
This commit is contained in:
parent
1c7aed71ec
commit
9daf8b4109
|
@ -453,6 +453,9 @@ class Query(BaseExpression):
|
||||||
# filtering against window functions is involved as it
|
# filtering against window functions is involved as it
|
||||||
# requires complex realising.
|
# requires complex realising.
|
||||||
annotation_mask = set()
|
annotation_mask = set()
|
||||||
|
if isinstance(self.group_by, tuple):
|
||||||
|
for expr in self.group_by:
|
||||||
|
annotation_mask |= expr.get_refs()
|
||||||
for aggregate in aggregates.values():
|
for aggregate in aggregates.values():
|
||||||
annotation_mask |= aggregate.get_refs()
|
annotation_mask |= aggregate.get_refs()
|
||||||
inner_query.set_annotation_mask(annotation_mask)
|
inner_query.set_annotation_mask(annotation_mask)
|
||||||
|
|
|
@ -15,3 +15,6 @@ Bugfixes
|
||||||
* Fixed a regression in Django 4.2 that caused a crash of
|
* Fixed a regression in Django 4.2 that caused a crash of
|
||||||
:class:`~django.contrib.postgres.search.SearchVector` function with ``%``
|
:class:`~django.contrib.postgres.search.SearchVector` function with ``%``
|
||||||
characters (:ticket:`34459`).
|
characters (:ticket:`34459`).
|
||||||
|
|
||||||
|
* Fixed a regression in Django 4.2 that caused aggregation over query that
|
||||||
|
uses explicit grouping to group against the wrong columns (:ticket:`34464`).
|
||||||
|
|
|
@ -36,6 +36,7 @@ from django.db.models.functions import (
|
||||||
Greatest,
|
Greatest,
|
||||||
Least,
|
Least,
|
||||||
Lower,
|
Lower,
|
||||||
|
Mod,
|
||||||
Now,
|
Now,
|
||||||
Pi,
|
Pi,
|
||||||
TruncDate,
|
TruncDate,
|
||||||
|
@ -2175,3 +2176,9 @@ class AggregateAnnotationPruningTests(TestCase):
|
||||||
sql = ctx.captured_queries[0]["sql"].lower()
|
sql = ctx.captured_queries[0]["sql"].lower()
|
||||||
self.assertEqual(sql.count("select"), 2, "Subquery wrapping required")
|
self.assertEqual(sql.count("select"), 2, "Subquery wrapping required")
|
||||||
self.assertEqual(sql.count("authors_count"), 2)
|
self.assertEqual(sql.count("authors_count"), 2)
|
||||||
|
|
||||||
|
def test_referenced_group_by_annotation_kept(self):
|
||||||
|
queryset = Book.objects.values(pages_mod=Mod("pages", 10)).annotate(
|
||||||
|
mod_count=Count("*")
|
||||||
|
)
|
||||||
|
self.assertEqual(queryset.count(), 1)
|
||||||
|
|
Loading…
Reference in New Issue