summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2019-01-15 23:39:49 -0600
committerTim Graham <timograham@gmail.com>2019-01-16 14:09:51 -0500
commit9e5e5a657b95ee49923fe3d2691c5d73813b4c53 (patch)
treee90cc99646c58a15b821ef3f656c9d2c656afa02
parent4fc35a9c3efdc9154efce28cb23cb84f8834517e (diff)
Fixed #30044 -- Raised a FieldError on inherited field update attempts.
-rw-r--r--django/db/models/sql/query.py2
-rw-r--r--tests/expressions/models.py4
-rw-r--r--tests/expressions/tests.py9
3 files changed, 13 insertions, 2 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 7d991b6b84..675ff8c176 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -1600,6 +1600,8 @@ class Query:
field_list = name.split(LOOKUP_SEP)
join_info = self.setup_joins(field_list, self.get_meta(), self.get_initial_alias(), can_reuse=reuse)
targets, final_alias, join_list = self.trim_joins(join_info.targets, join_info.joins, join_info.path)
+ if not allow_joins and len(join_list) > 1:
+ raise FieldError('Joined field references are not permitted in this query')
if len(targets) > 1:
raise FieldError("Referencing multicolumn fields with F() objects "
"isn't supported")
diff --git a/tests/expressions/models.py b/tests/expressions/models.py
index 42e4a37bb0..33f7850ac1 100644
--- a/tests/expressions/models.py
+++ b/tests/expressions/models.py
@@ -15,6 +15,10 @@ class Employee(models.Model):
return '%s %s' % (self.firstname, self.lastname)
+class RemoteEmployee(Employee):
+ adjusted_salary = models.IntegerField()
+
+
class Company(models.Model):
name = models.CharField(max_length=100)
num_employees = models.PositiveIntegerField()
diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py
index af432eb163..e66dcd6297 100644
--- a/tests/expressions/tests.py
+++ b/tests/expressions/tests.py
@@ -24,8 +24,8 @@ from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
from django.test.utils import Approximate
from .models import (
- UUID, UUIDPK, Company, Employee, Experiment, Number, Result, SimulationRun,
- Time,
+ UUID, UUIDPK, Company, Employee, Experiment, Number, RemoteEmployee,
+ Result, SimulationRun, Time,
)
@@ -285,6 +285,11 @@ class BasicExpressionsTests(TestCase):
with self.assertRaisesMessage(FieldError, msg):
test_gmbh.save()
+ def test_update_inherited_field_value(self):
+ msg = 'Joined field references are not permitted in this query'
+ with self.assertRaisesMessage(FieldError, msg):
+ RemoteEmployee.objects.update(adjusted_salary=F('salary') * 5)
+
def test_object_update_unsaved_objects(self):
# F expressions cannot be used to update attributes on objects which do
# not yet exist in the database