[1.5.x] Fixed #21203 -- resolve_columns fields misalignment

In queries using .defer() together with .select_related() the values
and fields arguments didn't align properly for resolve_columns().

Backpatch of 8c27247397 from master.
This commit is contained in:
Michael Manfre 2013-10-01 21:34:39 +03:00 committed by Anssi Kääriäinen
parent 997a332cbb
commit 7ebd10019d
3 changed files with 21 additions and 4 deletions

View File

@ -800,9 +800,8 @@ class SQLCompiler(object):
# into `resolve_columns` because it wasn't selected. # into `resolve_columns` because it wasn't selected.
only_load = self.deferred_to_columns() only_load = self.deferred_to_columns()
if only_load: if only_load:
db_table = self.query.model._meta.db_table fields = [f for f in fields if f.model._meta.db_table not in only_load or
fields = [f for f in fields if db_table in only_load and f.column in only_load[f.model._meta.db_table]]
f.column in only_load[db_table]]
if has_aggregate_select: if has_aggregate_select:
# pad None in to fields for aggregates # pad None in to fields for aggregates
fields = fields[:aggregate_start] + [ fields = fields[:aggregate_start] + [

View File

@ -401,3 +401,12 @@ class ModelA(models.Model):
name = models.TextField() name = models.TextField()
b = models.ForeignKey(ModelB, null=True) b = models.ForeignKey(ModelB, null=True)
d = models.ForeignKey(ModelD) d = models.ForeignKey(ModelD)
class Ticket21203Parent(models.Model):
parentid = models.AutoField(primary_key=True)
parent_bool = models.BooleanField(default=True)
created = models.DateTimeField(auto_now=True)
class Ticket21203Child(models.Model):
childid = models.AutoField(primary_key=True)
parent = models.ForeignKey(Ticket21203Parent)

View File

@ -23,7 +23,8 @@ from .models import (Annotation, Article, Author, Celebrity, Child, Cover,
Ranking, Related, Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten, Ranking, Related, Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten,
Node, ObjectA, ObjectB, ObjectC, CategoryItem, SimpleCategory, Node, ObjectA, ObjectB, ObjectC, CategoryItem, SimpleCategory,
SpecialCategory, OneToOneCategory, NullableName, ProxyCategory, SpecialCategory, OneToOneCategory, NullableName, ProxyCategory,
SingleObject, RelatedObject, ModelA, ModelD) SingleObject, RelatedObject, ModelA, ModelD,
Ticket21203Parent, Ticket21203Child)
class BaseQuerysetTest(TestCase): class BaseQuerysetTest(TestCase):
@ -2149,3 +2150,11 @@ class EmptyStringPromotionTests(TestCase):
self.assertIn('LEFT OUTER JOIN', str(qs.query)) self.assertIn('LEFT OUTER JOIN', str(qs.query))
else: else:
self.assertNotIn('LEFT OUTER JOIN', str(qs.query)) self.assertNotIn('LEFT OUTER JOIN', str(qs.query))
class Ticket21203Tests(TestCase):
def test_ticket_21203(self):
p = Ticket21203Parent.objects.create(parent_bool=True)
c = Ticket21203Child.objects.create(parent=p)
qs = Ticket21203Child.objects.select_related('parent').defer('parent__created')
self.assertQuerysetEqual(qs, [c], lambda x: x)
self.assertIs(qs[0].parent.parent_bool, True)