Browse Source

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

Jacob Walls 2 tháng trước cách đây
mục cha
commit
72ff18d41c
2 tập tin đã thay đổi với 11 bổ sung0 xóa
  1. 4 0
      django/db/models/sql/subqueries.py
  2. 7 0
      tests/composite_pk/test_update.py

+ 4 - 0
django/db/models/sql/subqueries.py

@@ -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 "

+ 7 - 0
tests/composite_pk/test_update.py

@@ -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))