[Django] #35643: Regression in Queryset sequence of value, annotate, order, value

39 views
Skip to first unread message

Django

unread,
Jul 30, 2024, 12:51:14 PM7/30/24
to django-...@googlegroups.com
#35643: Regression in Queryset sequence of value, annotate, order, value
-------------------------------------+-------------------------------------
Reporter: Gert Van Gool | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
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
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35643>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 30, 2024, 3:23:26 PM7/30/24
to django-...@googlegroups.com
#35643: Regression in Queryset sequence of value, annotate, order, value
-------------------------------------+-------------------------------------
Reporter: Gert Van Gool | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | 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):

* owner: (none) => Simon Charette
* severity: Normal => Release blocker
* status: new => assigned

Comment:

{{{#!python
======================================================================
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/pyt...@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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35643#comment:1>

Django

unread,
Jul 30, 2024, 11:59:41 PM7/30/24
to django-...@googlegroups.com
#35643: Regression in Queryset sequence of value, annotate, order, value
-------------------------------------+-------------------------------------
Reporter: Gert Van Gool | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* has_patch: 0 => 1
* stage: Unreviewed => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/35643#comment:2>

Django

unread,
Aug 2, 2024, 1:34:39 PM8/2/24
to django-...@googlegroups.com
#35643: Regression in Queryset sequence of value, annotate, order, value
-------------------------------------+-------------------------------------
Reporter: Gert Van Gool | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35643#comment:3>

Django

unread,
Aug 2, 2024, 3:21:22 PM8/2/24
to django-...@googlegroups.com
#35643: Regression in Queryset sequence of value, annotate, order, value
-------------------------------------+-------------------------------------
Reporter: Gert Van Gool | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"a16f13a8661297eda12c4177bb01fa2e5b5ccc56" a16f13a]:
{{{#!CommitTicketReference repository=""
revision="a16f13a8661297eda12c4177bb01fa2e5b5ccc56"
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.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35643#comment:4>

Django

unread,
Aug 2, 2024, 3:22:37 PM8/2/24
to django-...@googlegroups.com
#35643: Regression in Queryset sequence of value, annotate, order, value
-------------------------------------+-------------------------------------
Reporter: Gert Van Gool | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia <124304+nessita@…>):

In [changeset:"55f529270125e17bf428585734dbcb33332a46de" 55f5292]:
{{{#!CommitTicketReference repository=""
revision="55f529270125e17bf428585734dbcb33332a46de"
[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.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35643#comment:5>
Reply all
Reply to author
Forward
0 new messages