[Django] #36068: bulk_create() update_fields argument accepts fields participating in a composite primary key

7 views
Skip to first unread message

Django

unread,
Jan 6, 2025, 11:13:58 PM1/6/25
to django-...@googlegroups.com
#36068: bulk_create() update_fields argument accepts fields participating in a
composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Type:
| Cleanup/optimization
Status: new | Component: Database
| layer (models, ORM)
Version: dev | 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
-------------------------------------+-------------------------------------
`bulk_create()`
[https://github.com/django/django/blob/40d5516385448a73426aad396778f369a363eda9/tests/bulk_create/tests.py#L540
rejects] primary key fields provided to the `update_fields` argument. I
think I would expect any fields participating in a composite primary key
to also be subject to that constraint.

Failing test:

{{{#!diff
diff --git a/tests/composite_pk/test_create.py
b/tests/composite_pk/test_create.py
index 7c9925b946..77a84fc53d 100644
--- a/tests/composite_pk/test_create.py
+++ b/tests/composite_pk/test_create.py
@@ -77,6 +77,19 @@ class CompositePKCreateTests(TestCase):
self.assertEqual(obj_3.pk, (obj_3.tenant_id, obj_3.id))
self.assertEqual(obj_3.email, "user...@example.com")

+ def test_bulk_create_user_with_disallowed_update_fields(self):
+ objs = [User(tenant=self.tenant, id=8291,
email="user...@example.com")] * 2
+ msg = "bulk_create() cannot be used with primary keys in update
fields"
+ with self.assertRaisesMessage(ValueError, msg):
+ User.objects.bulk_create(
+ objs,
+ update_conflicts=True,
+ update_fields=["tenant_id"],
+ unique_fields=["id", "tenant_id"],
+ )
+ # instead produces:
+ # [<User: User object ((1, 8291))>, <User: User object ((1,
8291))>]
+
def test_get_or_create_user(self):
test_cases = (
{
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36068>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 6, 2025, 11:23:07 PM1/6/25
to django-...@googlegroups.com
#36068: bulk_create() update_fields argument accepts fields participating in a
composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | 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:

Looks like a straightforward change to `_check_bulk_create_options`
--
Ticket URL: <https://code.djangoproject.com/ticket/36068#comment:1>

Django

unread,
Jan 7, 2025, 6:32:20 AM1/7/25
to django-...@googlegroups.com
#36068: bulk_create() update_fields argument accepts fields participating in a
composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(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 Jacob Walls):

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

Comment:

[https://github.com/django/django/pull/19011 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36068#comment:2>

Django

unread,
Jan 9, 2025, 11:57:58 AM1/9/25
to django-...@googlegroups.com
#36068: bulk_create() update_fields argument accepts fields participating in a
composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(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 Sarah Boyce):

* stage: Accepted => Ready for checkin

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

Django

unread,
Jan 10, 2025, 2:23:48 AM1/10/25
to django-...@googlegroups.com
#36068: bulk_create() update_fields argument accepts fields participating in a
composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: closed
Component: Database layer | Version: dev
(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 Sarah Boyce <42296566+sarahboyce@…>):

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

Comment:

In [changeset:"0fb51ec5a0ab445aa178761f80f7415ac8098998" 0fb51ec]:
{{{#!CommitTicketReference repository=""
revision="0fb51ec5a0ab445aa178761f80f7415ac8098998"
Fixed #36068 -- Raised ValueError when providing a composite PK field to
bulk_create() update_fields.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36068#comment:4>
Reply all
Reply to author
Forward
0 new messages