[Django] #36349: Queryset.values() followed by annotate may crash depending of values parameters order

9 views
Skip to first unread message

Django

unread,
Apr 23, 2025, 5:34:50 AM4/23/25
to django-...@googlegroups.com
#36349: Queryset.values() followed by annotate may crash depending of values
parameters order
-------------------------------------+-------------------------------------
Reporter: Zankroh | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Normal
Keywords: values annotate | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Since Django 5.2, the order of values can crash if annotate is used after.

Imagine the following "AgentAmount" model:

{{{
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra
|
+---------------------+--------------+------+-----+---------+----------------+
| id | bigint | NO | PRI | NULL |
auto_increment |
| uid_agent | varchar(64) | NO | MUL | NULL |
|
| amount | int | NO | | NULL |
|
| status | varchar(60) | NO | MUL | NULL |
|
| last | tinyint(1) | NO | MUL | NULL |
|
| created_at | datetime(6) | NO | MUL | NULL |
|
+---------------------+--------------+------+-----+---------+----------------+
}}}


The following query will crash :
{{{#!python
AgentAmount.objects.filter(
uid_agent="SMP43279",
last=True
).values(
"created_at__date",
"status",
).annotate(
sum_amount=Sum("amount")
)
}}}

Here the traceback:

{{{
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\xxx\Projects\env\Lib\site-
packages\django\db\models\query.py", line 360, in __repr__
data = list(self[: REPR_OUTPUT_SIZE + 1])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\Projects\env\Lib\site-
packages\django\db\models\query.py", line 384, in __iter__
self._fetch_all()
File "C:\Users\xxx\Projects\env\Lib\site-
packages\django\db\models\query.py", line 1935, in _fetch_all
self._result_cache = list(self._iterable_class(self))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\Projects\env\Lib\site-
packages\django\db\models\query.py", line 216, in __iter__
for row in compiler.results_iter(
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\Projects\env\Lib\site-
packages\django\db\models\sql\compiler.py", line 1571, in results_iter
results = self.execute_sql(
^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\Projects\env\Lib\site-
packages\django\db\models\sql\compiler.py", line 1609, in execute_sql
sql, params = self.as_sql()
^^^^^^^^^^^^^
File "C:\Users\xxx\Projects\env\Lib\site-
packages\django\db\models\sql\compiler.py", line 765, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup(
^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\Projects\env\Lib\site-
packages\django\db\models\sql\compiler.py", line 85, in pre_sql_setup
self.setup_query(with_col_aliases=with_col_aliases)
File "C:\Users\xxx\Projects\env\Lib\site-
packages\django\db\models\sql\compiler.py", line 74, in setup_query
self.select, self.klass_info, self.annotation_col_map =
self.get_select(
^^^^^^^^^^^^^^^^
File "C:\Users\xxx\Projects\env\Lib\site-
packages\django\db\models\sql\compiler.py", line 286, in get_select
expression = cols[expression]
~~~~^^^^^^^^^^^^
IndexError: tuple index out of range
}}}


But if I do this query

{{{#!python
models.AgentHistory.objects.filter(
uid_agent="SMP43279",
last=True
).values(
"status",
"created_at__date",
).annotate(
sum_amount=Sum("amount")
)
}}}
Everything is fine
--
Ticket URL: <https://code.djangoproject.com/ticket/36349>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 23, 2025, 5:36:57 AM4/23/25
to django-...@googlegroups.com
#36349: Queryset.values() followed by annotate may crash depending of values
parameters order
-------------------------------------+-------------------------------------
Reporter: Zankroh | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: values annotate | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Zankroh:

Old description:
New description:
"status",
"created_at__date",
).annotate(
sum_amount=Sum("amount")
)
}}}
Everything is fine

--
--
Ticket URL: <https://code.djangoproject.com/ticket/36349#comment:1>

Django

unread,
Apr 23, 2025, 9:15:19 AM4/23/25
to django-...@googlegroups.com
#36349: Queryset.values() followed by annotate may crash depending of values
parameters order
-------------------------------------+-------------------------------------
Reporter: Zankroh | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: values annotate | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* resolution: => duplicate
* status: new => closed

Comment:

Duplicate of #36292 (''Annotating an aggregate function over a group
including annotations or transforms followed by a column references
crashes with `IndexError`'') fixed by
543e17c4405dfdac4f18759fc78b190406d14239 in Django 5.2.1 meant to be
released on May 7th.
--
Ticket URL: <https://code.djangoproject.com/ticket/36349#comment:2>
Reply all
Reply to author
Forward
0 new messages