mirror of
https://github.com/django/django.git
synced 2025-03-12 18:30:48 +00:00
[5.2.x] Fixed #36120 -- Raised FieldError when targeting a composite primary key field with QuerySet.update().
Backport of 72ff18d41cfb004ae180bdf87fd8bad93041c691 from main.
This commit is contained in:
parent
720ef7a867
commit
791ed4fd97
@ -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 "
|
||||
|
@ -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))
|
||||
|
Loading…
x
Reference in New Issue
Block a user