diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index ce11716..911872f 100644
a
|
b
|
class Query(object):
|
104 | 104 | self.alias_map = {} # Maps alias to join information |
105 | 105 | self.table_map = {} # Maps table names to list of aliases. |
106 | 106 | self.join_map = {} |
107 | | self.rev_join_map = {} # Reverse of join_map. |
108 | 107 | self.quote_cache = {} |
109 | 108 | self.default_cols = True |
110 | 109 | self.default_ordering = True |
… |
… |
class Query(object):
|
244 | 243 | obj.alias_map = self.alias_map.copy() |
245 | 244 | obj.table_map = self.table_map.copy() |
246 | 245 | obj.join_map = self.join_map.copy() |
247 | | obj.rev_join_map = self.rev_join_map.copy() |
248 | 246 | obj.quote_cache = {} |
249 | 247 | obj.default_cols = self.default_cols |
250 | 248 | obj.default_ordering = self.default_ordering |
… |
… |
class Query(object):
|
463 | 461 | change_map = {} |
464 | 462 | used = set() |
465 | 463 | conjunction = (connector == AND) |
| 464 | # Add the joins in the rhs query into the new query. |
466 | 465 | first = True |
467 | 466 | for alias in rhs.tables: |
468 | 467 | if not rhs.alias_refcount[alias]: |
469 | 468 | # An unused alias. |
470 | 469 | continue |
471 | | promote = (rhs.alias_map[alias][JOIN_TYPE] == self.LOUTER) |
472 | | lhs, table, lhs_col, col = rhs.rev_join_map[alias] |
| 470 | table, _, join_type, lhs, lhs_col, col, _ = rhs.alias_map[alias] |
| 471 | promote = join_type == self.LOUTER |
473 | 472 | # If the left side of the join was already relabeled, use the |
474 | 473 | # updated alias. |
475 | 474 | lhs = change_map.get(lhs, lhs) |
476 | 475 | new_alias = self.join((lhs, table, lhs_col, col), |
477 | | (conjunction and not first), used, promote, not conjunction) |
| 476 | conjunction and not first, used, promote, not conjunction) |
478 | 477 | used.add(new_alias) |
479 | 478 | change_map[alias] = new_alias |
480 | 479 | first = False |
… |
… |
class Query(object):
|
766 | 765 | col.relabel_aliases(change_map) |
767 | 766 | |
768 | 767 | # 2. Rename the alias in the internal table/alias datastructures. |
| 768 | for k, aliases in self.join_map.items(): |
| 769 | aliases = tuple([change_map.get(a, a) for a in aliases]) |
| 770 | self.join_map[k] = aliases |
769 | 771 | for old_alias, new_alias in change_map.iteritems(): |
770 | 772 | alias_data = list(self.alias_map[old_alias]) |
771 | 773 | alias_data[RHS_ALIAS] = new_alias |
772 | | |
773 | | t = self.rev_join_map[old_alias] |
774 | | data = list(self.join_map[t]) |
775 | | data[data.index(old_alias)] = new_alias |
776 | | self.join_map[t] = tuple(data) |
777 | | self.rev_join_map[new_alias] = t |
778 | | del self.rev_join_map[old_alias] |
| 774 | |
779 | 775 | self.alias_refcount[new_alias] = self.alias_refcount[old_alias] |
780 | 776 | del self.alias_refcount[old_alias] |
781 | 777 | self.alias_map[new_alias] = tuple(alias_data) |
… |
… |
class Query(object):
|
923 | 919 | self.join_map[t_ident] += (alias,) |
924 | 920 | else: |
925 | 921 | self.join_map[t_ident] = (alias,) |
926 | | self.rev_join_map[alias] = t_ident |
927 | 922 | return alias |
928 | 923 | |
929 | 924 | def setup_inherited_models(self): |