Fixed #28408 -- Added error message when updating with annotated expressions on joined fields.

Co-Authored-By: Simon Charette <charette.s@gmail.com>
This commit is contained in:
can 2019-06-27 22:13:48 +03:00 committed by Mariusz Felisiak
parent fb54aca540
commit f03b7bd114
2 changed files with 31 additions and 11 deletions

View file

@ -1,5 +1,6 @@
from django.core.exceptions import FieldError
from django.db.models import Count, F, Max
from django.db.models.functions import Concat, Lower
from django.test import TestCase
from .models import A, B, Bar, D, DataPoint, Foo, RelatedPoint
@ -182,16 +183,19 @@ class AdvancedTests(TestCase):
# Update where annotation is used for filtering
qs = DataPoint.objects.annotate(related_count=Count('relatedpoint'))
self.assertEqual(qs.filter(related_count=1).update(value='Foo'), 1)
# Update where annotation is used in update parameters
# #26539 - This isn't forbidden but also doesn't generate proper SQL
# qs = RelatedPoint.objects.annotate(data_name=F('data__name'))
# updated = qs.update(name=F('data_name'))
# self.assertEqual(updated, 1)
# Update where aggregation annotation is used in update parameters
qs = RelatedPoint.objects.annotate(max=Max('data__value'))
msg = (
'Aggregate functions are not allowed in this query '
'(name=Max(Col(update_datapoint, update.DataPoint.value))).'
)
msg = 'Joined field references are not permitted in this query'
with self.assertRaisesMessage(FieldError, msg):
qs.update(name=F('max'))
def test_update_with_joined_field_annotation(self):
msg = 'Joined field references are not permitted in this query'
for annotation in (
F('data__name'),
Lower('data__name'),
Concat('data__name', 'data__value'),
):
with self.subTest(annotation=annotation):
with self.assertRaisesMessage(FieldError, msg):
RelatedPoint.objects.annotate(new_name=annotation).update(name=F('new_name'))