[Django] #36481: UpdateQuery.add_update_values always computes "direct" variable as True

9 views
Skip to first unread message

Django

unread,
Jun 26, 2025, 1:54:24 AMJun 26
to django-...@googlegroups.com
#36481: UpdateQuery.add_update_values always computes "direct" variable as True
-------------------------------------+-------------------------------------
Reporter: Ryan P Kilby | 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
-------------------------------------+-------------------------------------
For context, I've been messing around with `ForeignObject` in an attempt
to use correlated subqueries as a join condition. e.g., defining a
`latest_post` relation field that enables queries like
`Author.objects.select_related("latest_post")`. Given that the relation is
based on a subquery, the field is not concrete, and in testing how the
queryset API works with this field, I ran into a confusing condition in
`QuerySet.update` via the `UpdateQuery.add_update_values` method. In
short,
[[https://github.com/django/django/blob/5.2.3/django/db/models/sql/subqueries.py#L90|line
90]] computes a `direct` variable that always resolves to true. You can
verify how the boolean logic resolves with:

{{{#!python
class UpdateTests(unittest.TestCase):

def test_direct(self):
testcases = [
(True, True),
(True, False),
(False, True),
(False, False),
]
for auto_created, concrete in testcases:
with self.subTest(auto_created=auto_created,
concrete=concrete):
self.assertTrue(not (auto_created and not concrete) or not
concrete)
}}}

This condition should simplify to `not auto_created or concrete or not
concrete` to `concrete or not concrete` to `True`. Digging through the
blame, direct was previously provided by the model meta's
`.get_field_by_name()`, which
[[https://github.com/django/django/blob/749d23251bbd6564341405e6f8c1da129b8307e7/django/db/models/options.py#L410-L411|states]]
"direct is True if the field exists on this model". Without fully
understanding the field internals, I assume `direct` should have been
replaced with just `field.concrete`. I'm not sure what the intent was for
auto-created fields, but whether it's concrete seems to be the actual
relevant bit, not whether it's auto-created.
--
Ticket URL: <https://code.djangoproject.com/ticket/36481>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 26, 2025, 2:06:22 AMJun 26
to django-...@googlegroups.com
#36481: UpdateQuery.add_update_values always computes "direct" variable as True
-------------------------------------+-------------------------------------
Reporter: Ryan P Kilby | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ryan P Kilby):

* has_patch: 0 => 1

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

Django

unread,
Jun 26, 2025, 5:04:16 AMJun 26
to django-...@googlegroups.com
#36481: UpdateQuery.add_update_values always computes "direct" variable as True
-------------------------------------+-------------------------------------
Reporter: Ryan P Kilby | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | 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 Sarah Boyce):

* stage: Unreviewed => Accepted

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

Django

unread,
Jun 26, 2025, 5:09:57 AMJun 26
to django-...@googlegroups.com
#36481: UpdateQuery.add_update_values always computes "direct" variable as True
-------------------------------------+-------------------------------------
Reporter: Ryan P Kilby | Owner: Ryan P
| Kilby
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | 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 Sarah Boyce):

* owner: (none) => Ryan P Kilby
* status: new => assigned

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

Django

unread,
Jun 26, 2025, 3:20:50 PMJun 26
to django-...@googlegroups.com
#36481: UpdateQuery.add_update_values always computes "direct" variable as True
-------------------------------------+-------------------------------------
Reporter: Ryan P Kilby | Owner: Ryan P
| Kilby
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36481#comment:4>

Django

unread,
Sep 2, 2025, 4:35:12 PM (5 days ago) Sep 2
to django-...@googlegroups.com
#36481: UpdateQuery.add_update_values always computes "direct" variable as True
-------------------------------------+-------------------------------------
Reporter: Ryan P Kilby | Owner: Ryan P
| Kilby
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 1 => 0
* needs_tests: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36481#comment:5>

Django

unread,
Sep 4, 2025, 9:39:52 AM (3 days ago) Sep 4
to django-...@googlegroups.com
#36481: UpdateQuery.add_update_values always computes "direct" variable as True
-------------------------------------+-------------------------------------
Reporter: Ryan P Kilby | Owner: Ryan P
| Kilby
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | 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 Jacob Walls):

* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36481#comment:6>

Django

unread,
Sep 4, 2025, 12:15:56 PM (3 days ago) Sep 4
to django-...@googlegroups.com
#36481: UpdateQuery.add_update_values always computes "direct" variable as True
-------------------------------------+-------------------------------------
Reporter: Ryan P Kilby | Owner: Ryan P
| Kilby
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | 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
-------------------------------------+-------------------------------------
Comment (by Jacob Walls <jacobtylerwalls@…>):

In [changeset:"11c2c9ac17db1c04c6de302167d4b0a5539c90fd" 11c2c9a]:
{{{#!CommitTicketReference repository=""
revision="11c2c9ac17db1c04c6de302167d4b0a5539c90fd"
Refs #36481 -- Improved test coverage for invalid updates on reverse
relations.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36481#comment:7>

Django

unread,
Sep 4, 2025, 12:15:56 PM (3 days ago) Sep 4
to django-...@googlegroups.com
#36481: UpdateQuery.add_update_values always computes "direct" variable as True
-------------------------------------+-------------------------------------
Reporter: Ryan P Kilby | Owner: Ryan P
| Kilby
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | 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 Jacob Walls <jacobtylerwalls@…>):

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

Comment:

In [changeset:"bad03eb108b029dad70cbd997f1fef221da3e415" bad03eb]:
{{{#!CommitTicketReference repository=""
revision="bad03eb108b029dad70cbd997f1fef221da3e415"
Fixed #36481 -- Fixed QuerySet.update concrete fields check.

FieldError is now emitted for invalid update calls involving reverse
relations, where previously they failed with AttributeError.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36481#comment:8>
Reply all
Reply to author
Forward
0 new messages