[Django] #36299: ORM Regressions in Django 5.2

10 views
Skip to first unread message

Django

unread,
Apr 4, 2025, 9:23:23 AM4/4/25
to django-...@googlegroups.com
#36299: ORM Regressions in Django 5.2
-------------------------------------+-------------------------------------
Reporter: OutOfFocus4 | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | 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
-------------------------------------+-------------------------------------
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:

{{{#!python
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`. 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'`
--
Ticket URL: <https://code.djangoproject.com/ticket/36299>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 4, 2025, 9:23:39 AM4/4/25
to django-...@googlegroups.com
#36299: ORM Regressions in Django 5.2
-------------------------------------+-------------------------------------
Reporter: OutOfFocus4 | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | 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 OutOfFocus4):

* Attachment "tests.py" added.

Django

unread,
Apr 4, 2025, 9:26:30 AM4/4/25
to django-...@googlegroups.com
#36299: ORM Regressions in Django 5.2
-------------------------------------+-------------------------------------
Reporter: OutOfFocus4 | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by OutOfFocus4:

Old description:

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

New description:

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:

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

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

--
--
Ticket URL: <https://code.djangoproject.com/ticket/36299#comment:1>

Django

unread,
Apr 4, 2025, 9:57:31 AM4/4/25
to django-...@googlegroups.com
#36299: ORM Regressions in Django 5.2
-------------------------------------+-------------------------------------
Reporter: OutOfFocus4 | 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):

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

Comment:

Regression in #28900 (65ad4ade74dc9208b9d686a451cd6045df0c9c3a)

Thanks for the detailed report.

> 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:
> ...
> This code adds the alias to selected regardless of the value of the
select parameter.

That's effectively the origin of the problem, thanks for investigating.

Currently looking into the second one with `select_for_update`.
--
Ticket URL: <https://code.djangoproject.com/ticket/36299#comment:2>
Reply all
Reply to author
Forward
0 new messages