Fixed #21150 -- select_related + annotate join promotion failure

Added tests for a .annotate().select_related() join promotion failure.
This happened to work on master but was currently untested.
This commit is contained in:
Anssi Kääriäinen 2013-09-24 18:23:02 +03:00
parent 20472aa827
commit ed0d720b78
2 changed files with 30 additions and 2 deletions

View File

@ -87,3 +87,14 @@ class HardbackBook(Book):
def __str__(self): def __str__(self):
return "%s (hardback): %s" % (self.name, self.weight) return "%s (hardback): %s" % (self.name, self.weight)
# Models for ticket #21150
class Alfa(models.Model):
pass
class Bravo(models.Model):
pass
class Charlie(models.Model):
alfa = models.ForeignKey(Alfa, null=True)
bravo = models.ForeignKey(Bravo, null=True)

View File

@ -12,8 +12,9 @@ from django.test import TestCase, skipUnlessDBFeature
from django.test.utils import Approximate from django.test.utils import Approximate
from django.utils import six from django.utils import six
from .models import (Author, Book, Publisher, Clues, Entries, HardbackBook, from .models import (
ItemTag, WithManualPK) Author, Book, Publisher, Clues, Entries, HardbackBook, ItemTag,
WithManualPK, Alfa, Bravo, Charlie)
class AggregationTests(TestCase): class AggregationTests(TestCase):
@ -1135,3 +1136,19 @@ class AggregationTests(TestCase):
'select__sum': 10, 'select__sum': 10,
'select__avg': Approximate(1.666, places=2), 'select__avg': Approximate(1.666, places=2),
}) })
def test_ticket_21150(self):
b = Bravo.objects.create()
c = Charlie.objects.create(bravo=b)
qs = Charlie.objects.select_related('alfa').annotate(Count('bravo__charlie'))
self.assertQuerysetEqual(
qs, [c], lambda x: x)
self.assertIs(qs[0].alfa, None)
a = Alfa.objects.create()
c.alfa = a
c.save()
# Force re-evaluation
qs = qs.all()
self.assertQuerysetEqual(
qs, [c], lambda x: x)
self.assertEqual(qs[0].alfa, a)