Opened 5 months ago

Last modified 5 months ago

#36349 closed Bug

Queryset.values() followed by annotate may crash depending of values parameters order — at Initial Version

Reported by: Zankroh Owned by:
Component: Database layer (models, ORM) Version: 5.2
Severity: Normal Keywords: values annotate
Cc: Zankroh Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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 :

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

models.AgentHistory.objects.filter(
    uid_agent="SMP43279",
    last=True
).values(
    "status",
    "created_at__date",
).annotate(
    sum_amount=Sum("amount")
)

Everything is fine

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top