Fixed a problem when constructing complex select_related() calls.

Avoids joining with the wrong tables when connecting select_related() tables to
the main query. This also leads to slightly more efficient (meaning less tables
are joined) SQL queries in some other cases, too. Some unnecessary tables are
now trimmed that were not previously.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7741 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-06-26 01:02:11 +00:00
parent 1834428648
commit 588eeb356c
2 changed files with 57 additions and 13 deletions

View File

@ -631,8 +631,10 @@ class Query(object):
# We have to do the same "final join" optimisation as in # We have to do the same "final join" optimisation as in
# add_filter, since the final column might not otherwise be part of # add_filter, since the final column might not otherwise be part of
# the select set (so we can't order on it). # the select set (so we can't order on it).
while 1:
join = self.alias_map[alias] join = self.alias_map[alias]
if col == join[RHS_JOIN_COL]: if col != join[RHS_JOIN_COL]:
break
self.unref_alias(alias) self.unref_alias(alias)
alias = join[LHS_ALIAS] alias = join[LHS_ALIAS]
col = join[LHS_JOIN_COL] col = join[LHS_JOIN_COL]
@ -830,6 +832,10 @@ class Query(object):
if not always_create: if not always_create:
for alias in self.join_map.get(t_ident, ()): for alias in self.join_map.get(t_ident, ()):
if alias not in exclusions: if alias not in exclusions:
if lhs_table and not self.alias_refcount[self.alias_map[alias][LHS_ALIAS]]:
# The LHS of this join tuple is no longer part of the
# query, so skip this possibility.
continue
self.ref_alias(alias) self.ref_alias(alias)
if promote: if promote:
self.promote_alias(alias) self.promote_alias(alias)
@ -989,13 +995,15 @@ class Query(object):
col = target.column col = target.column
alias = join_list[-1] alias = join_list[-1]
if final > 1: while final > 1:
# An optimization: if the final join is against the same column as # An optimization: if the final join is against the same column as
# we are comparing against, we can go back one step in the join # we are comparing against, we can go back one step in the join
# chain and compare against the lhs of the join instead. The result # chain and compare against the lhs of the join instead (and then
# (potentially) involves one less table join. # repeat the optimization). The result, potentially, involves less
# table joins.
join = self.alias_map[alias] join = self.alias_map[alias]
if col == join[RHS_JOIN_COL]: if col != join[RHS_JOIN_COL]:
break
self.unref_alias(alias) self.unref_alias(alias)
alias = join[LHS_ALIAS] alias = join[LHS_ALIAS]
col = join[LHS_JOIN_COL] col = join[LHS_JOIN_COL]

View File

@ -135,6 +135,24 @@ class ManagedModel(models.Model):
def __unicode__(self): def __unicode__(self):
return self.data return self.data
# An inter-related setup with multiple paths from Child to Detail.
class Detail(models.Model):
data = models.CharField(max_length=10)
class MemberManager(models.Manager):
def get_query_set(self):
return super(MemberManager, self).get_query_set().select_related("details")
class Member(models.Model):
name = models.CharField(max_length=10)
details = models.OneToOneField(Detail, primary_key=True)
objects = MemberManager()
class Child(models.Model):
person = models.OneToOneField(Member, primary_key=True)
parent = models.ForeignKey(Member, related_name="children")
__test__ = {'API_TESTS':""" __test__ = {'API_TESTS':"""
>>> t1 = Tag(name='t1') >>> t1 = Tag(name='t1')
@ -720,5 +738,23 @@ appropriately.
>>> Report.objects.values_list("creator__extra__info", flat=True).order_by("name") >>> Report.objects.values_list("creator__extra__info", flat=True).order_by("name")
[u'e1', u'e2', None] [u'e1', u'e2', None]
Similarly for select_related(), joins beyond an initial nullable join must
use outer joins so that all results are included.
>>> Report.objects.select_related("creator", "creator__extra").order_by("name")
[<Report: r1>, <Report: r2>, <Report: r3>]
When there are multiple paths to a table from another table, we have to be
careful not to accidentally reuse an inappropriate join when using
select_related(). We used to return the parent's Detail record here by mistake.
>>> d1 = Detail.objects.create(data="d1")
>>> d2 = Detail.objects.create(data="d2")
>>> m1 = Member.objects.create(name="m1", details=d1)
>>> m2 = Member.objects.create(name="m2", details=d2)
>>> c1 = Child.objects.create(person=m2, parent=m1)
>>> obj = m1.children.select_related("person__details")[0]
>>> obj.person.details.data
u'd2'
"""} """}