#33091: A FieldError should be raised when trying to update MTI inherited field
with F reference to child field
-------------------------------------+-------------------------------------
               Reporter:  Shai       |          Owner:  nobody
  Berger                             |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  dev
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 This is the converse of #30044.

 In that bug, we made sure a proper error was raised when we do
 {{{#!python
 ChildModel.objects.update(child_field=F('parent_field'))
 }}}
 We should similarly make sure a proper error is raised when we do
 {{{#!python
 ChildModel.objects.update(parent_field=F('child_field'))
 }}}
 With current main, if we add the obvious test
 {{{#!diff
 diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py
 index 33c5189390..0a97d537fe 100644
 --- a/tests/expressions/tests.py
 +++ b/tests/expressions/tests.py
 @@ -318,6 +318,11 @@ class BasicExpressionsTests(TestCase):
          with self.assertRaisesMessage(FieldError, msg):
              RemoteEmployee.objects.update(adjusted_salary=F('salary') *
 5)

 +    def test_update_set_inherited_field_from_child(self):
 +        msg = 'Joined field references are not permitted in this query'
 +        with self.assertRaisesMessage(FieldError, msg):
 +            RemoteEmployee.objects.update(salary=F('adjusted_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
 }}}
 it fails
 {{{
 ======================================================================
 FAIL: test_update_set_inherited_field_from_child
 (expressions.tests.BasicExpressionsTests)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/usr/lib/python3.9/unittest/case.py", line 59, in testPartExecutor
     yield
   File "/usr/lib/python3.9/unittest/case.py", line 592, in run
     self._callTestMethod(testMethod)
   File "/usr/lib/python3.9/unittest/case.py", line 550, in _callTestMethod
     method()
   File "/home/django/django/tests/expressions/tests.py", line 324, in
 test_update_set_inherited_field_from_child
     RemoteEmployee.objects.update(salary=F('adjusted_salary') / 5)
   File "/usr/lib/python3.9/contextlib.py", line 137, in __exit__
     self.gen.throw(typ, value, traceback)
   File "/home/django/django/django/test/testcases.py", line 687, in
 _assert_raises_or_warns_cm
     self.assertIn(expected_message, str(getattr(cm, cm_attr)))
   File "/usr/lib/python3.9/unittest/case.py", line 1096, in assertIn
     self.fail(self._formatMessage(msg, standardMsg))
   File "/usr/lib/python3.9/unittest/case.py", line 668, in fail
     raise self.failureException(msg)
 AssertionError: 'Joined field references are not permitted in this query'
 not found in "Cannot resolve keyword 'adjusted_salary' into field. Choices
 are: company_ceo_set, company_point_of_contact_set, firstname, id,
 lastname, manager, manager_id, remoteemployee, salary"

 ----------------------------------------------------------------------
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33091>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/048.37f8f94b12b92177e09047c8a43e3785%40djangoproject.com.

Reply via email to