diff options
| author | Simon Charette <charette.s@gmail.com> | 2025-01-25 19:13:13 -0500 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-01-28 12:05:53 +0000 |
| commit | efec74b90868c2e611f863bf4301d92ce08067e8 (patch) | |
| tree | d5379982569d0179ab8081a357e0bd3c96edf918 | |
| parent | 0ba35a49481c9fec4731ca0dd2230d8d48f51389 (diff) | |
Fixed #36122 -- Raised FieldError when updating with composite reference value.
Thanks Jacob Walls for the report and test.
| -rw-r--r-- | django/db/models/sql/compiler.py | 5 | ||||
| -rw-r--r-- | tests/composite_pk/test_update.py | 9 |
2 files changed, 14 insertions, 0 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 6f90f11f1b..04372c509e 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -2019,6 +2019,11 @@ class SQLUpdateCompiler(SQLCompiler): "Window expressions are not allowed in this query " "(%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"): if field.remote_field: val = val.prepare_database_save(field) diff --git a/tests/composite_pk/test_update.py b/tests/composite_pk/test_update.py index 5bc53f1fe1..697383b007 100644 --- a/tests/composite_pk/test_update.py +++ b/tests/composite_pk/test_update.py @@ -1,5 +1,6 @@ from django.core.exceptions import FieldError from django.db import connection +from django.db.models import F from django.test import TestCase from .models import Comment, Tenant, TimeStamped, Token, User @@ -182,3 +183,11 @@ class CompositePKUpdateTests(TestCase): msg = "Composite primary key fields must be updated individually." with self.assertRaisesMessage(FieldError, msg): 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")) |
