Opened 29 hours ago
Last modified 22 hours ago
#36068 assigned Cleanup/optimization
bulk_create() update_fields argument accepts fields participating in a composite primary key
Reported by: | Jacob Walls | Owned by: | Jacob Walls |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
bulk_create()
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:
-
tests/composite_pk/test_create.py
diff --git a/tests/composite_pk/test_create.py b/tests/composite_pk/test_create.py index 7c9925b946..77a84fc53d 100644
a b class CompositePKCreateTests(TestCase): 77 77 self.assertEqual(obj_3.pk, (obj_3.tenant_id, obj_3.id)) 78 78 self.assertEqual(obj_3.email, "user8214@example.com") 79 79 80 def test_bulk_create_user_with_disallowed_update_fields(self): 81 objs = [User(tenant=self.tenant, id=8291, email="user8291@example.com")] * 2 82 msg = "bulk_create() cannot be used with primary keys in update fields" 83 with self.assertRaisesMessage(ValueError, msg): 84 User.objects.bulk_create( 85 objs, 86 update_conflicts=True, 87 update_fields=["tenant_id"], 88 unique_fields=["id", "tenant_id"], 89 ) 90 # instead produces: 91 # [<User: User object ((1, 8291))>, <User: User object ((1, 8291))>] 92 80 93 def test_get_or_create_user(self): 81 94 test_cases = ( 82 95 {
Note:
See TracTickets
for help on using tickets.
Looks like a straightforward change to
_check_bulk_create_options