1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #19513, #18580 -- Fixed crash on QuerySet.update() after annotate().

This commit is contained in:
David Sanders
2016-04-17 10:03:08 -07:00
committed by Tim Graham
parent 06acb3445f
commit a84344bc53
3 changed files with 28 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
from __future__ import unicode_literals
from django.core.exceptions import FieldError
from django.db.models import F, Max
from django.db.models import Count, F, Max
from django.test import TestCase
from .models import A, B, Bar, D, DataPoint, Foo, RelatedPoint
@@ -158,3 +158,23 @@ class AdvancedTests(TestCase):
qs = DataPoint.objects.annotate(max=Max('value'))
with self.assertRaisesMessage(FieldError, 'Aggregate functions are not allowed in this query'):
qs.update(another_value=F('max'))
def test_update_annotated_multi_table_queryset(self):
"""
Update of a queryset that's been annotated and involves multiple tables.
"""
# Trivial annotated update
qs = DataPoint.objects.annotate(related_count=Count('relatedpoint'))
self.assertEqual(qs.update(value='Foo'), 3)
# 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'))
with self.assertRaisesMessage(FieldError, 'Aggregate functions are not allowed in this query'):
qs.update(name=F('max'))