#36299 closed Bug (fixed)
Calling alias after values_list adds the aliased value to the result set
Description (last modified by ) ¶
ORM calls that worked prior to Django 5.2 now return different results or outright fail in Django 5.2. I have attached a file with proof-of-concept tests.
The first issue I noticed is that calling alias
on after values_list
adds the aliased value to the result set. I believe the root of this error is in django/db/models/sql/query.py
lines 1224 and 1225:
if self.selected: self.selected[alias] = alias
This code adds the alias to selected
regardless of the value of the select
parameter.
Another issue I found causes queryset evaluation to raise an AttributeError
if the database backend supports SELECT ... FOR UPDATE
. The following code:
with atomic(): values = ( User.objects.select_for_update(of=("self",)) .values_list( Concat(F("first_name"), Value(" "), F("last_name")), "email" ) .get(pk=12) )
will fail with a stacktrace ending in AttributeError: 'Concat' object has no attribute 'target'
using the postgresql
DB backend.
Change History (11)
by , 11 days ago
comment:1 by , 11 days ago
Description: | modified (diff) |
---|
comment:2 by , 11 days ago
Owner: | set to |
---|---|
Severity: | Normal → Release blocker |
Status: | new → assigned |
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 11 days ago
Summary: | ORM Regressions in Django 5.2 → Calling alias after values_list adds the aliased value to the result set |
---|
Thank you for the report
Simon you're too quick!
-
TabularUnified tests/queries/tests.py
diff --git a/tests/queries/tests.py b/tests/queries/tests.py index c429a93af3..f9e831da15 100644
a b from operator import attrgetter 7 7 from django.core.exceptions import EmptyResultSet, FieldError, FullResultSet 8 8 from django.db import DEFAULT_DB_ALIAS, connection 9 9 from django.db.models import CharField, Count, Exists, F, Max, OuterRef, Q 10 from django.db.models.expressions import RawSQL 10 from django.db.models.expressions import RawSQL, Value 11 11 from django.db.models.functions import ExtractYear, Length, LTrim 12 12 from django.db.models.sql.constants import LOUTER 13 13 from django.db.models.sql.where import AND, OR, NothingNode, WhereNode … … class ValuesQuerysetTests(TestCase): 2737 2737 qs = qs.values_list("num") 2738 2738 self.assertSequenceEqual(qs, [(72,)]) 2739 2739 2740 def test_values_list_with_alias(self): 2741 qs = Number.objects.values_list("num").alias(extra=Value(1)) 2742 self.assertSequenceEqual(qs, [(72,)]) 2743 2740 2744 def test_flat_extra_values_list(self): 2741 2745 # testing for ticket 14930 issues
Replicated the alias issue and confirmed it's a regression in 65ad4ade74dc9208b9d686a451cd6045df0c9c3a
If select_for_update is a separate issue, we should create a separate ticket
comment:4 by , 11 days ago
Replicated the second issue, also a regression in 65ad4ade74dc9208b9d686a451cd6045df0c9c3a
-
TabularUnified tests/select_for_update/tests.py
diff --git a/tests/select_for_update/tests.py b/tests/select_for_update/tests.py index e8ba8f8b6e..18fca277cb 100644
a b from django.db import ( 13 13 router, 14 14 transaction, 15 15 ) 16 from django.db.models import F, Value 17 from django.db.models.functions import Concat 16 18 from django.test import ( 17 19 TransactionTestCase, 18 20 override_settings, … … class SelectForUpdateTests(TransactionTestCase): 122 124 list(Person.objects.select_for_update(no_key=True)) 123 125 self.assertIs(self.has_for_update_sql(ctx.captured_queries, no_key=True), True) 124 126 127 @skipUnlessDBFeature("has_select_for_update_of") 128 def test_for_update_of_values_list(self): 129 with transaction.atomic(): 130 values = ( 131 Person.objects.select_for_update( 132 of=("self",), 133 ) 134 .values_list(Concat(Value("Dr. "), F("name")), "born") 135 ).get(pk=self.person.pk) 136 self.assertTupleEqual(values, ("Dr. Reinhardt", self.city1.pk)) 137 125 138 @skipUnlessDBFeature("has_select_for_update_of")
comment:5 by , 11 days ago
Thanks for the triage Sarah, I'll create a ticket for the second one.
Here's a PR for the alias issue Jeff. I gave you co-author attribute since you pointed at the exact solution and provided tests but I can remove it if you'd prefer just LMK on Github.
comment:6 by , 11 days ago
Has patch: | set |
---|
comment:8 by , 10 days ago
Triage Stage: | Accepted → Ready for checkin |
---|
Regression in #28900 (65ad4ade74dc9208b9d686a451cd6045df0c9c3a)
Thanks for the detailed report.
That's effectively the origin of the problem, thanks for investigating.
Currently looking into the second one with
select_for_update
.