[Django] #33463: bulk_update does not work with plain F('...') assigment

23 views
Skip to first unread message

Django

unread,
Jan 27, 2022, 6:10:57 AM1/27/22
to django-...@googlegroups.com
#33463: bulk_update does not work with plain F('...') assigment
-----------------------------------------+-------------------------------
Reporter: jerch | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 4.0
Severity: Normal | Keywords: bulk_update F
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+-------------------------------
Repro:

* assign plain F(...) to some model instance field
* save with bulk_update

Example:
{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
>>> from exampleapp.models import SelfRef
>>> o = SelfRef.objects.all().first()
>>> o.c8 = F('name') # model has char fields 'c8' and 'name'
>>> SelfRef.objects.bulk_update([o], ['c8'])
1
>>> o.refresh_from_db()
>>> o.c8
'F(name)'
>>> from django.db import connection
>>> connection.queries[-2]
{'sql': 'UPDATE "exampleapp_selfref" SET "c8" = CASE WHEN
("exampleapp_selfref"."id" = 1290012) THEN \'F(name)\' ELSE NULL END WHERE
"exampleapp_selfref"."id" IN (1290012)', 'time': '0.001'}
}}}
}}}

The created SQL contains the string repr of F(), instead of resolving to
the column name. Looking at the source code, the culprit seems to be a too
narrow type check in
https://github.com/django/django/blob/2eed554c3fd75dae1beade79f357ffd18d3c4fdf/django/db/models/query.py#L673.

It works, if the type check gets replaced by one of these:
{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
# either do duck type testing
if not hasattr(attr, 'resolve_expression'):
...
# or test for F explicitly:
if not isinstance(attr, (Expression, F)):
...
}}}
}}}

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

Django

unread,
Jan 27, 2022, 7:19:03 AM1/27/22
to django-...@googlegroups.com
#33463: bulk_update does not work with plain F('...') assigment
-------------------------------------+-------------------------------------

Reporter: jerch | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:

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

* version: 4.0 => dev
* component: Uncategorized => Database layer (models, ORM)


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

Django

unread,
Jan 27, 2022, 7:31:04 AM1/27/22
to django-...@googlegroups.com
#33463: bulk_update() does not work with plain F('...') expressions.
-------------------------------------+-------------------------------------
Reporter: jerch | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: bulk_update F | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Tom Forbes (added)
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted


Comment:

Thanks for the report. Agreed, we should check `resolve_expression` like
elsewhere.

Would you like to provide a patch?

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

Django

unread,
Jan 27, 2022, 8:05:15 AM1/27/22
to django-...@googlegroups.com
#33463: bulk_update() does not work with plain F('...') expressions.
-------------------------------------+-------------------------------------
Reporter: jerch | Owner: nobody

Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: bulk_update F | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by jerch):

Sure, can do a patch. Is this worth to add a test case as well?

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

Django

unread,
Jan 27, 2022, 8:50:31 AM1/27/22
to django-...@googlegroups.com
#33463: bulk_update() does not work with plain F('...') expressions.
-------------------------------------+-------------------------------------
Reporter: jerch | Owner: nobody

Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: bulk_update F | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jerch):

* has_patch: 0 => 1


Comment:

Patch PR link: https://github.com/django/django/pull/15368

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

Django

unread,
Jan 27, 2022, 1:06:59 PM1/27/22
to django-...@googlegroups.com
#33463: bulk_update() does not work with plain F('...') expressions.
-------------------------------------+-------------------------------------
Reporter: jerch | Owner: nobody

Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: bulk_update F | 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/33463#comment:5>

Django

unread,
Jan 27, 2022, 1:08:57 PM1/27/22
to django-...@googlegroups.com
#33463: bulk_update() does not work with plain F('...') expressions.
-------------------------------------+-------------------------------------
Reporter: jerch | Owner: jerch
Type: Bug | Status: assigned

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: bulk_update F | 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):

* owner: nobody => jerch
* status: new => assigned


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

Django

unread,
Jan 27, 2022, 2:04:02 PM1/27/22
to django-...@googlegroups.com
#33463: bulk_update() does not work with plain F('...') expressions.
-------------------------------------+-------------------------------------
Reporter: jerch | Owner: jerch
Type: Bug | Status: closed

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: bulk_update F | 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@…>):

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


Comment:

In [changeset:"0af9a5fc7d765aa05ea784e2c3237675f3bb4b49" 0af9a5fc]:
{{{
#!CommitTicketReference repository=""
revision="0af9a5fc7d765aa05ea784e2c3237675f3bb4b49"
Fixed #33463 -- Fixed QuerySet.bulk_update() with F() expressions.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33463#comment:7>

Reply all
Reply to author
Forward
0 new messages