[Django] #34827: select_for_update example raises TransactionManagementError

5 views
Skip to first unread message

Django

unread,
Sep 10, 2023, 10:48:16 PM9/10/23
to django-...@googlegroups.com
#34827: select_for_update example raises TransactionManagementError
-----------------------------------------+------------------------
Reporter: SydneyUni-Jim | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 4.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-----------------------------------------+------------------------
The example for `select_for_update` at
https://docs.djangoproject.com/en/4.2/ref/models/querysets/#select-for-
update shows `select-for-update` being used before starting the
transaction. With Django 4.2.5, this raises a TransactionManagementError.

{{{
Traceback (most recent call last):
File "/site-packages/django/db/models/query.py", line 633, in get
num = len(clone)
File "/site-packages/django/db/models/query.py", line 380, in __len__
self._fetch_all()
File "/site-packages/django/db/models/query.py", line 1881, in
_fetch_all
self._result_cache = list(self._iterable_class(self))
File "/site-packages/django/db/models/query.py", line 91, in __iter__
results = compiler.execute_sql(
File "/site-packages/django/db/models/sql/compiler.py", line 1549, in
execute_sql
sql, params = self.as_sql()
File "/site-packages/django/db/models/sql/compiler.py", line 816, in
as_sql
raise TransactionManagementError(
django.db.transaction.TransactionManagementError: select_for_update cannot
be used outside of a transaction.
}}}

I think the sample should be:

{{{
#!python
from django.db import transaction

with transaction.atomic():
entries =
Entry.objects.select_for_update().filter(author=request.user)
for entry in entries:
...
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34827>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 10, 2023, 11:02:59 PM9/10/23
to django-...@googlegroups.com
#34827: select_for_update example raises TransactionManagementError
-------------------------------+--------------------------------------

Reporter: SydneyUni-Jim | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 4.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by SydneyUni-Jim:

Old description:

New description:

update shows `select_for_update` being used before starting the


transaction. With Django 4.2.5, this raises a TransactionManagementError.

{{{
Traceback (most recent call last):
File "/site-packages/django/db/models/query.py", line 633, in get
num = len(clone)
File "/site-packages/django/db/models/query.py", line 380, in __len__
self._fetch_all()
File "/site-packages/django/db/models/query.py", line 1881, in
_fetch_all
self._result_cache = list(self._iterable_class(self))
File "/site-packages/django/db/models/query.py", line 91, in __iter__
results = compiler.execute_sql(
File "/site-packages/django/db/models/sql/compiler.py", line 1549, in
execute_sql
sql, params = self.as_sql()
File "/site-packages/django/db/models/sql/compiler.py", line 816, in
as_sql
raise TransactionManagementError(
django.db.transaction.TransactionManagementError: select_for_update cannot
be used outside of a transaction.
}}}

I think the example should be:

{{{
#!python
from django.db import transaction

with transaction.atomic():
entries =
Entry.objects.select_for_update().filter(author=request.user)
for entry in entries:
...
}}}

--

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

Django

unread,
Sep 10, 2023, 11:12:33 PM9/10/23
to django-...@googlegroups.com
#34827: select_for_update example raises TransactionManagementError
-------------------------------+--------------------------------------
Reporter: SydneyUni-Jim | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 4.2
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Simon Charette):

* status: new => closed
* resolution: => invalid


Comment:

Querysets are lazily evaluated, and thus only the evaluating part needs to
be run within a transaction

> When the queryset is evaluated (**for entry in entries in this case**),
all matched entries will be locked until the end of the transaction block

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

Reply all
Reply to author
Forward
0 new messages