Opened 4 months ago

Closed 3 months ago

Last modified 3 months ago

#35643 closed Bug (fixed)

Regression in Queryset sequence of value, annotate, order, value

Reported by: Gert Van Gool Owned by: Simon Charette
Component: Database layer (models, ORM) Version: 5.1
Severity: Release blocker Keywords:
Cc: Gert Van Gool Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Testing the 5.1RC on our project, I noticed a failure in a queryset that worked before (Django 5.0.7)

I've added a test case:

diff --git i/tests/queries/tests.py w/tests/queries/tests.py
index 7ac8a65d42..c19d877ffc 100644
--- i/tests/queries/tests.py
+++ w/tests/queries/tests.py
@@ -1375,6 +1375,16 @@ class Queries1Tests(TestCase):
         self.assertCountEqual(items_after, [self.i2, self.i3, self.i4])
         self.assertCountEqual(items_before, items_after)

+    def test_values_count_value(self):
+        self.assertSequenceEqual(
+            Tag.objects.all()
+            .values("category")
+            .annotate(Count("category"))
+            .order_by("-category__count")
+            .values_list("category", flat=True),
+            [self.nc1.id, None],
+        )
+

 class Queries2Tests(TestCase):
     @classmethod

Change History (5)

comment:1 by Simon Charette, 4 months ago

Owner: set to Simon Charette
Severity: NormalRelease blocker
Status: newassigned
======================================================================
ERROR: test_values_count_value (queries.tests.Queries1Tests.test_values_count_value)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/charettes/Workspace/django/django/db/models/sql/query.py", line 1889, in transform
    return self.try_transform(wrapped, name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/charettes/Workspace/django/django/db/models/sql/query.py", line 1426, in try_transform
    raise FieldError(
django.core.exceptions.FieldError: Unsupported lookup 'count' for AutoField or join on the field not permitted, perhaps you meant contains?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/charettes/Workspace/django/tests/queries/tests.py", line 1389, in test_values_count_value
    self.assertSequenceEqual(
  File "/usr/local/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/unittest/case.py", line 1003, in assertSequenceEqual
    len1 = len(seq1)
           ^^^^^^^^^
  File "/Users/charettes/Workspace/django/django/db/models/query.py", line 363, in __len__
    self._fetch_all()
  File "/Users/charettes/Workspace/django/django/db/models/query.py", line 1909, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/charettes/Workspace/django/django/db/models/query.py", line 268, in __iter__
    for row in compiler.results_iter(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/charettes/Workspace/django/django/db/models/sql/compiler.py", line 1531, in results_iter
    results = self.execute_sql(
              ^^^^^^^^^^^^^^^^^
  File "/Users/charettes/Workspace/django/django/db/models/sql/compiler.py", line 1567, in execute_sql
    sql, params = self.as_sql()
                  ^^^^^^^^^^^^^
  File "/Users/charettes/Workspace/django/django/db/models/sql/compiler.py", line 756, in as_sql
    extra_select, order_by, group_by = self.pre_sql_setup(
                                       ^^^^^^^^^^^^^^^^^^^
  File "/Users/charettes/Workspace/django/django/db/models/sql/compiler.py", line 85, in pre_sql_setup
    order_by = self.get_order_by()
               ^^^^^^^^^^^^^^^^^^^
  File "/Users/charettes/Workspace/django/django/db/models/sql/compiler.py", line 480, in get_order_by
    for expr, is_ref in self._order_by_pairs():
  File "/Users/charettes/Workspace/django/django/db/models/sql/compiler.py", line 463, in _order_by_pairs
    yield from self.find_ordering_name(
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/charettes/Workspace/django/django/db/models/sql/compiler.py", line 1109, in find_ordering_name
    (OrderBy(transform_function(t, alias), descending=descending), False)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/charettes/Workspace/django/django/db/models/sql/query.py", line 1893, in transform
    raise last_field_exception
  File "/Users/charettes/Workspace/django/django/db/models/sql/query.py", line 1866, in setup_joins
    path, final_field, targets, rest = self.names_to_path(
                                       ^^^^^^^^^^^^^^^^^^^
  File "/Users/charettes/Workspace/django/django/db/models/sql/query.py", line 1771, in names_to_path
    raise FieldError(
django.core.exceptions.FieldError: Cannot resolve keyword 'count' into field. Choices are: dumbcategory_ptr, dumbcategory_ptr_id, id, name, tag

Bisected to b0ad41198b3e333f57351e3fce5a1fb47f23f376. The problem here is that Aggregation.default_alias generates an alias that is composed of LOOKUP_SEP. In other words, the added logic didn't account for annotations containing a lookup separator to exists.

Last edited 4 months ago by Simon Charette (previous) (diff)

comment:2 by Simon Charette, 4 months ago

Has patch: set
Triage Stage: UnreviewedAccepted

comment:3 by Natalia Bidart, 3 months ago

Triage Stage: AcceptedReady for checkin

comment:4 by GitHub <noreply@…>, 3 months ago

Resolution: fixed
Status: assignedclosed

In a16f13a:

Fixed #35643 -- Fixed a crash when ordering a QuerySet by a reference containing "".

Regression in b0ad41198b3e333f57351e3fce5a1fb47f23f376.

Refs #34013. The initial logic did not consider that annotation aliases
can include lookup or transform separators.

Thanks Gert Van Gool for the report and Mariusz Felisiak for the review.

comment:5 by Natalia <124304+nessita@…>, 3 months ago

In 55f5292:

[5.1.x] Fixed #35643 -- Fixed a crash when ordering a QuerySet by a reference containing "".

Regression in b0ad41198b3e333f57351e3fce5a1fb47f23f376.

Refs #34013. The initial logic did not consider that annotation aliases
can include lookup or transform separators.

Thanks Gert Van Gool for the report and Mariusz Felisiak for the review.
Backport of a16f13a8661297eda12c4177bb01fa2e5b5ccc56 from main.

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