1
0
mirror of https://github.com/django/django.git synced 2025-03-12 02:12:38 +00:00

Fixed #36120 -- Raised FieldError when targeting a composite primary key field with QuerySet.update().

This commit is contained in:
Jacob Walls 2025-01-22 22:21:09 -05:00 committed by Sarah Boyce
parent d9af197801
commit 72ff18d41c
2 changed files with 11 additions and 0 deletions

View File

@ -90,6 +90,10 @@ class UpdateQuery(Query):
not (field.auto_created and not field.concrete) or not field.concrete
)
model = field.model._meta.concrete_model
if field.name == "pk" and model._meta.is_composite_pk:
raise FieldError(
"Composite primary key fields must be updated individually."
)
if not direct or (field.is_relation and field.many_to_many):
raise FieldError(
"Cannot update model field %r (only non-relations and "

View File

@ -1,3 +1,4 @@
from django.core.exceptions import FieldError
from django.db import connection
from django.test import TestCase
@ -175,3 +176,9 @@ class CompositePKUpdateTests(TestCase):
with self.assertRaisesMessage(ValueError, msg):
Comment.objects.update(user=User())
def test_cant_update_pk_field(self):
qs = Comment.objects.filter(user__email=self.user_1.email)
msg = "Composite primary key fields must be updated individually."
with self.assertRaisesMessage(FieldError, msg):
qs.update(pk=(1, 10))