Opened 28 hours ago

Last modified 20 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):  
    7777        self.assertEqual(obj_3.pk, (obj_3.tenant_id, obj_3.id))
    7878        self.assertEqual(obj_3.email, "user8214@example.com")
    7979
     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
    8093    def test_get_or_create_user(self):
    8194        test_cases = (
    8295            {

Change History (2)

comment:1 by Simon Charette, 28 hours ago

Triage Stage: UnreviewedAccepted

Looks like a straightforward change to _check_bulk_create_options

comment:2 by Jacob Walls, 20 hours ago

Has patch: set
Owner: set to Jacob Walls
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top