summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2025-04-02 23:20:53 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2025-04-03 17:51:26 +0200
commit543e17c4405dfdac4f18759fc78b190406d14239 (patch)
tree8b0080a3b22b05bed463d7ba88a17e488e8e3e5a
parent3ae049b26b995c650c41ef918d5f60beed52b4ba (diff)
Fixed #36292 -- Fixed crash when aggregating over a group mixing transforms and references.
Regression in 65ad4ade74dc9208b9d686a451cd6045df0c9c3a. Refs #28900 Thanks Patrick Altman for the report.
-rw-r--r--django/db/models/sql/query.py3
-rw-r--r--docs/releases/5.2.1.txt4
-rw-r--r--tests/aggregation/tests.py27
3 files changed, 33 insertions, 1 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 0d1fe5fb43..9fde8496e9 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -2339,6 +2339,9 @@ class Query(BaseExpression):
self.append_annotation_mask(group_by_annotations)
self.select = tuple(values_select.values())
self.values_select = tuple(values_select)
+ if self.selected is not None:
+ for index, value_select in enumerate(values_select):
+ self.selected[value_select] = index
group_by = list(self.select)
for alias, annotation in self.annotation_select.items():
if not (group_by_cols := annotation.get_group_by_cols()):
diff --git a/docs/releases/5.2.1.txt b/docs/releases/5.2.1.txt
index 16cbd2c9ba..a4e73841bd 100644
--- a/docs/releases/5.2.1.txt
+++ b/docs/releases/5.2.1.txt
@@ -9,4 +9,6 @@ Django 5.2.1 fixes several bugs in 5.2.
Bugfixes
========
-* ...
+* Fixed a regression in Django 5.2 that caused a crash when annotating
+ aggregate expressions over query that uses explicit grouping by transforms
+ followed by field references (:ticket:`36292`).
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py
index 8caefc060c..a8ce6ed1d8 100644
--- a/tests/aggregation/tests.py
+++ b/tests/aggregation/tests.py
@@ -2210,6 +2210,33 @@ class AggregateTestCase(TestCase):
},
)
+ def test_group_by_transform_column(self):
+ self.assertSequenceEqual(
+ Store.objects.values(
+ "original_opening__date",
+ "name",
+ )
+ .annotate(Count("books"))
+ .order_by("name"),
+ [
+ {
+ "original_opening__date": datetime.date(1994, 4, 23),
+ "name": "Amazon.com",
+ "books__count": 6,
+ },
+ {
+ "original_opening__date": datetime.date(2001, 3, 15),
+ "name": "Books.com",
+ "books__count": 4,
+ },
+ {
+ "original_opening__date": datetime.date(1945, 4, 25),
+ "name": "Mamma and Pappa's Books",
+ "books__count": 3,
+ },
+ ],
+ )
+
def test_group_by_reference_subquery(self):
author_qs = (
Author.objects.annotate(publisher_id=F("book__publisher"))