mirror of
https://github.com/django/django.git
synced 2025-05-22 14:56:31 +00:00
[5.2.x] Fixed #36122 -- Raised FieldError when updating with composite reference value.
Thanks Jacob Walls for the report and test. Backport of efec74b90868c2e611f863bf4301d92ce08067e8 from main.
This commit is contained in:
parent
9fe17213a6
commit
d4d2e09f19
@ -2019,6 +2019,11 @@ class SQLUpdateCompiler(SQLCompiler):
|
|||||||
"Window expressions are not allowed in this query "
|
"Window expressions are not allowed in this query "
|
||||||
"(%s=%r)." % (field.name, val)
|
"(%s=%r)." % (field.name, val)
|
||||||
)
|
)
|
||||||
|
if isinstance(val, ColPairs):
|
||||||
|
raise FieldError(
|
||||||
|
"Composite primary keys expressions are not allowed "
|
||||||
|
"in this query (%s=F('pk'))." % field.name
|
||||||
|
)
|
||||||
elif hasattr(val, "prepare_database_save"):
|
elif hasattr(val, "prepare_database_save"):
|
||||||
if field.remote_field:
|
if field.remote_field:
|
||||||
val = val.prepare_database_save(field)
|
val = val.prepare_database_save(field)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from django.core.exceptions import FieldError
|
from django.core.exceptions import FieldError
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
|
from django.db.models import F
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from .models import Comment, Tenant, TimeStamped, Token, User
|
from .models import Comment, Tenant, TimeStamped, Token, User
|
||||||
@ -182,3 +183,11 @@ class CompositePKUpdateTests(TestCase):
|
|||||||
msg = "Composite primary key fields must be updated individually."
|
msg = "Composite primary key fields must be updated individually."
|
||||||
with self.assertRaisesMessage(FieldError, msg):
|
with self.assertRaisesMessage(FieldError, msg):
|
||||||
qs.update(pk=(1, 10))
|
qs.update(pk=(1, 10))
|
||||||
|
|
||||||
|
def test_update_value_not_composite(self):
|
||||||
|
msg = (
|
||||||
|
"Composite primary keys expressions are not allowed in this "
|
||||||
|
"query (text=F('pk'))."
|
||||||
|
)
|
||||||
|
with self.assertRaisesMessage(FieldError, msg):
|
||||||
|
Comment.objects.update(text=F("pk"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user