[Django] #36301: select_for_update(of) crash when used with values_list since Django 5.2 on Postgres

14 views
Skip to first unread message

Django

unread,
Apr 4, 2025, 12:44:58 PM4/4/25
to django-...@googlegroups.com
#36301: select_for_update(of) crash when used with values_list since Django 5.2 on
Postgres
-------------------------------------+-------------------------------------
Reporter: Simon | Owner: Simon Charette
Charette |
Type: Bug | Status: assigned
Component: Database | Version: 5.2
layer (models, ORM) |
Severity: Release | Keywords:
blocker |
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
This was extracted from #36299 as a distinct issue.

-----

Queryset evaluation raises an `AttributeError` if the database backend
supports `SELECT ... FOR UPDATE`. The following code:

{{{#!python
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.

Replicate by Sarah as a regression in
65ad4ade74dc9208b9d686a451cd6045df0c9c3a

{{{#!diff
diff --git a/tests/select_for_update/tests.py
b/tests/select_for_update/tests.py
index e8ba8f8b6e..18fca277cb 100644
--- a/tests/select_for_update/tests.py
+++ b/tests/select_for_update/tests.py
@@ -13,6 +13,8 @@ from django.db import (
router,
transaction,
)
+from django.db.models import F, Value
+from django.db.models.functions import Concat
from django.test import (
TransactionTestCase,
override_settings,
@@ -122,6 +124,17 @@ class SelectForUpdateTests(TransactionTestCase):
list(Person.objects.select_for_update(no_key=True))
self.assertIs(self.has_for_update_sql(ctx.captured_queries,
no_key=True), True)

+ @skipUnlessDBFeature("has_select_for_update_of")
+ def test_for_update_of_values_list(self):
+ with transaction.atomic():
+ values = (
+ Person.objects.select_for_update(
+ of=("self",),
+ )
+ .values_list(Concat(Value("Dr. "), F("name")), "born")
+ ).get(pk=self.person.pk)
+ self.assertTupleEqual(values, ("Dr. Reinhardt", self.city1.pk))
+
@skipUnlessDBFeature("has_select_for_update_of")
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36301>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 4, 2025, 12:45:51 PM4/4/25
to django-...@googlegroups.com
#36301: select_for_update(of) crash when used with values_list since Django 5.2 on
Postgres
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted

Comment:

Self-accepting since #36299 was accepted once the regression was
identified.
--
Ticket URL: <https://code.djangoproject.com/ticket/36301#comment:1>

Django

unread,
Apr 4, 2025, 3:24:35 PM4/4/25
to django-...@googlegroups.com
#36301: select_for_update(of) crash when used with values_list since Django 5.2 on
Postgres
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(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

Comment:

[https://github.com/django/django/pull/19343 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36301#comment:2>

Django

unread,
Apr 7, 2025, 4:56:41 PM4/7/25
to django-...@googlegroups.com
#36301: select_for_update(of) crash when used with values_list since Django 5.2 on
Postgres
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(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 Mariusz Felisiak):

* stage: Accepted => Ready for checkin

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

Django

unread,
Apr 7, 2025, 5:49:05 PM4/7/25
to django-...@googlegroups.com
#36301: select_for_update(of) crash when used with values_list since Django 5.2 on
Postgres
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(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 Mariusz Felisiak <felisiak.mariusz@…>):

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

Comment:

In [changeset:"71a19a0e475165dbc14c1fe02f552013ee670e4c" 71a19a0]:
{{{#!CommitTicketReference repository=""
revision="71a19a0e475165dbc14c1fe02f552013ee670e4c"
Fixed #36301 -- Fixed select_for_update(of) crash when using
values()/values_list().

Regression in 65ad4ade74dc9208b9d686a451cd6045df0c9c3a which allowed for
annotations to be SELECT'ed before model field references through
values()/values_list() and broke assumptions the select_for_update(of)
table infererence logic had about model fields always being first.

Refs #28900.

Thanks OutOfFocus4 for the report and Sarah for the test.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36301#comment:4>

Django

unread,
Apr 7, 2025, 5:49:53 PM4/7/25
to django-...@googlegroups.com
#36301: select_for_update(of) crash when used with values_list since Django 5.2 on
Postgres
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(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 Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"5d2a0c51d459379447563af3789a6d941ca4947c" 5d2a0c5]:
{{{#!CommitTicketReference repository=""
revision="5d2a0c51d459379447563af3789a6d941ca4947c"
[5.2.x] Fixed #36301 -- Fixed select_for_update(of) crash when using
values()/values_list().

Regression in 65ad4ade74dc9208b9d686a451cd6045df0c9c3a which allowed for
annotations to be SELECT'ed before model field references through
values()/values_list() and broke assumptions the select_for_update(of)
table infererence logic had about model fields always being first.

Refs #28900.

Thanks OutOfFocus4 for the report and Sarah for the test.

Backport of 71a19a0e475165dbc14c1fe02f552013ee670e4c from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36301#comment:5>
Reply all
Reply to author
Forward
0 new messages