[Django] #36120: Calling .update() on the virtual "pk" field for composite primary keys raises AttributeError

19 views
Skip to first unread message

Django

unread,
Jan 20, 2025, 9:00:10 PM1/20/25
to django-...@googlegroups.com
#36120: Calling .update() on the virtual "pk" field for composite primary keys
raises AttributeError
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Release
| blocker
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Do we want to support `.update(pk=...` for composite primary keys?

If not, let's raise a friendly error.

Rough test for demo purposes:
{{{#!diff
diff --git a/tests/composite_pk/test_update.py
b/tests/composite_pk/test_update.py
index ec770230fc..6164e0de90 100644
--- a/tests/composite_pk/test_update.py
+++ b/tests/composite_pk/test_update.py
@@ -1,4 +1,5 @@
from django.db import connection
+from django.db.models import F
from django.test import TestCase

from .models import Comment, Tenant, TimeStamped, Token, User
@@ -175,3 +176,7 @@ class CompositePKUpdateTests(TestCase):

with self.assertRaisesMessage(ValueError, msg):
Comment.objects.update(user=User())
+
+ def test_update_lhs_pk(self):
+ qs = Comment.objects.filter(user__email=self.user_1.email)
+ qs.update(pk=(1, 100)) # supported?
}}}

----
{{{#!diff
======================================================================
ERROR: test_update_lhs_pk
(composite_pk.test_update.CompositePKUpdateTests.test_update_lhs_pk)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/source/django/tests/composite_pk/test_update.py", line 182,
in test_update_lhs_pk
qs.update(pk=(1, 100)) # decide whether to support this or not
~~~~~~~~~^^^^^^^^^^^^^
File "/Users/source/django/django/db/models/query.py", line 1254, in
update
rows = query.get_compiler(self.db).execute_sql(ROW_COUNT)
File "/Users/source/django/django/db/models/sql/compiler.py", line 2068,
in execute_sql
row_count = super().execute_sql(result_type)
File "/Users/source/django/django/db/models/sql/compiler.py", line 1610,
in execute_sql
sql, params = self.as_sql()
~~~~~~~~~~~^^
File "/Users/source/django/django/db/models/sql/compiler.py", line 2044,
in as_sql
values.append("%s = %s" % (qn(name), placeholder))
~~^^^^^^
File "/Users/source/django/django/db/models/sql/compiler.py", line 568,
in quote_name_unless_alias
r = self.connection.ops.quote_name(name)
File "/Users/source/django/django/db/backends/sqlite3/operations.py",
line 190, in quote_name
if name.startswith('"') and name.endswith('"'):
^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'startswith'

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

Django

unread,
Jan 20, 2025, 9:00:35 PM1/20/25
to django-...@googlegroups.com
#36120: Calling .update() on the virtual "pk" field for composite primary keys
raises AttributeError
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | 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 Jacob Walls:

Old description:
New description:

Do we want to support `.update(pk=...` for composite primary keys?

If not, let's raise a friendly error.

Rough test for demo purposes:
{{{#!diff
diff --git a/tests/composite_pk/test_update.py
b/tests/composite_pk/test_update.py
index ec770230fc..6164e0de90 100644
--- a/tests/composite_pk/test_update.py
+++ b/tests/composite_pk/test_update.py
@@ -1,4 +1,5 @@
from django.db import connection
--
Ticket URL: <https://code.djangoproject.com/ticket/36120#comment:1>

Django

unread,
Jan 20, 2025, 9:04:30 PM1/20/25
to django-...@googlegroups.com
index ec770230fc..3bd65d918a 100644
--- a/tests/composite_pk/test_update.py
+++ b/tests/composite_pk/test_update.py
@@ -175,3 +175,7 @@ class CompositePKUpdateTests(TestCase):
Ticket URL: <https://code.djangoproject.com/ticket/36120#comment:2>

Django

unread,
Jan 20, 2025, 9:28:06 PM1/20/25
to django-...@googlegroups.com
#36120: Calling .update() on the virtual "pk" field for composite primary keys
raises AttributeError
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
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:

Likely easier to raise here than to adapt `SQLUpdateCompiler`. A good spot
to intercept composite primary key assignment is likely
`UpdateQuery.add_update_values` which already intercepts relational and
many-to-many field updates.
--
Ticket URL: <https://code.djangoproject.com/ticket/36120#comment:3>

Django

unread,
Jan 22, 2025, 3:03:56 AM1/22/25
to django-...@googlegroups.com
#36120: Calling .update() on the virtual "pk" field for composite primary keys
raises AttributeError
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
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
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce):

#36122 is possibly related
--
Ticket URL: <https://code.djangoproject.com/ticket/36120#comment:4>

Django

unread,
Jan 22, 2025, 10:29:01 PM1/22/25
to django-...@googlegroups.com
#36120: Calling .update() on the virtual "pk" field for composite primary keys
raises AttributeError
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
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 Jacob Walls):

* has_patch: 0 => 1
* owner: (none) => Jacob Walls
* status: new => assigned

Comment:

[https://github.com/django/django/pull/19092 PR]

> #36122 is possibly related

Unfortunately the proposed fix does not close #36122.
--
Ticket URL: <https://code.djangoproject.com/ticket/36120#comment:5>

Django

unread,
Jan 28, 2025, 5:04:27 AM1/28/25
to django-...@googlegroups.com
#36120: Calling .update() on the virtual "pk" field for composite primary keys
raises AttributeError
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
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 Sarah Boyce):

* stage: Accepted => Ready for checkin

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

Django

unread,
Jan 28, 2025, 5:19:59 AM1/28/25
to django-...@googlegroups.com
#36120: Calling .update() on the virtual "pk" field for composite primary keys
raises AttributeError
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
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 Sarah Boyce <42296566+sarahboyce@…>):

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

Comment:

In [changeset:"72ff18d41cfb004ae180bdf87fd8bad93041c691" 72ff18d4]:
{{{#!CommitTicketReference repository=""
revision="72ff18d41cfb004ae180bdf87fd8bad93041c691"
Fixed #36120 -- Raised FieldError when targeting a composite primary key
field with QuerySet.update().
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36120#comment:7>

Django

unread,
Jan 28, 2025, 5:21:31 AM1/28/25
to django-...@googlegroups.com
#36120: Calling .update() on the virtual "pk" field for composite primary keys
raises AttributeError
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
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 Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"791ed4fd97a1a58401cd911dd69a32808fb86f79" 791ed4fd]:
{{{#!CommitTicketReference repository=""
revision="791ed4fd97a1a58401cd911dd69a32808fb86f79"
[5.2.x] Fixed #36120 -- Raised FieldError when targeting a composite
primary key field with QuerySet.update().

Backport of 72ff18d41cfb004ae180bdf87fd8bad93041c691 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36120#comment:8>
Reply all
Reply to author
Forward
0 new messages