Made sure join_field is always available in .join()

Refs #19385
This commit is contained in:
Anssi Kääriäinen 2012-12-17 00:43:33 +02:00
parent a04df803a5
commit 9ef3cab40b
2 changed files with 8 additions and 5 deletions

View File

@ -636,10 +636,10 @@ class SQLCompiler(object):
int_opts = int_model._meta int_opts = int_model._meta
continue continue
lhs_col = int_opts.parents[int_model].column lhs_col = int_opts.parents[int_model].column
link_field = int_opts.get_ancestor_link(int_model)
int_opts = int_model._meta int_opts = int_model._meta
alias = self.query.join((alias, int_opts.db_table, lhs_col, alias = self.query.join((alias, int_opts.db_table, lhs_col,
int_opts.pk.column), int_opts.pk.column), promote=promote, join_field=link_field)
promote=promote)
alias_chain.append(alias) alias_chain.append(alias)
else: else:
alias = root_alias alias = root_alias
@ -685,10 +685,11 @@ class SQLCompiler(object):
int_opts = int_model._meta int_opts = int_model._meta
continue continue
lhs_col = int_opts.parents[int_model].column lhs_col = int_opts.parents[int_model].column
link_field = int_opts.get_ancestor_link(int_model)
int_opts = int_model._meta int_opts = int_model._meta
alias = self.query.join( alias = self.query.join(
(alias, int_opts.db_table, lhs_col, int_opts.pk.column), (alias, int_opts.db_table, lhs_col, int_opts.pk.column),
promote=True promote=True, join_field=link_field
) )
alias_chain.append(alias) alias_chain.append(alias)
alias = self.query.join( alias = self.query.join(

View File

@ -944,6 +944,7 @@ class Query(object):
The 'join_field' is the field we are joining along (if any). The 'join_field' is the field we are joining along (if any).
""" """
lhs, table, lhs_col, col = connection lhs, table, lhs_col, col = connection
assert lhs is None or join_field is not None
existing = self.join_map.get(connection, ()) existing = self.join_map.get(connection, ())
if reuse is None: if reuse is None:
reuse = existing reuse = existing
@ -1003,8 +1004,9 @@ class Query(object):
for field, model in opts.get_fields_with_model(): for field, model in opts.get_fields_with_model():
if model not in seen: if model not in seen:
link_field = opts.get_ancestor_link(model) link_field = opts.get_ancestor_link(model)
seen[model] = self.join((root_alias, model._meta.db_table, seen[model] = self.join(
link_field.column, model._meta.pk.column)) (root_alias, model._meta.db_table, link_field.column,
model._meta.pk.column), join_field=link_field)
self.included_inherited_models = seen self.included_inherited_models = seen
def remove_inherited_models(self): def remove_inherited_models(self):