1
0
mirror of https://github.com/django/django.git synced 2025-03-12 18:30:48 +00:00

Fixed #36068 -- Raised ValueError when providing a composite PK field to bulk_create() update_fields.

This commit is contained in:
Jacob Walls 2025-01-07 06:28:57 -05:00 committed by Sarah Boyce
parent af6336f2c8
commit 0fb51ec5a0
2 changed files with 17 additions and 2 deletions

View File

@ -728,7 +728,7 @@ class QuerySet(AltersData):
"bulk_create() can only be used with concrete fields in "
"update_fields."
)
if any(f.primary_key for f in update_fields):
if any(f in self.model._meta.pk_fields for f in update_fields):
raise ValueError(
"bulk_create() cannot be used with primary keys in "
"update_fields."

View File

@ -1,4 +1,4 @@
from django.test import TestCase
from django.test import TestCase, skipUnlessDBFeature
from .models import Tenant, User
@ -77,6 +77,21 @@ class CompositePKCreateTests(TestCase):
self.assertEqual(obj_3.pk, (obj_3.tenant_id, obj_3.id))
self.assertEqual(obj_3.email, "user8214@example.com")
@skipUnlessDBFeature(
"supports_update_conflicts",
"supports_update_conflicts_with_target",
)
def test_bulk_create_user_with_pk_field_in_update_fields(self):
objs = [User(tenant=self.tenant, id=8291, email="user8291@example.com")]
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"],
)
def test_get_or_create_user(self):
test_cases = (
{