From bb9261197996464edc6398d194ec4e0b00d1c555 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Fri, 25 Apr 2008 15:01:40 +0000 Subject: [PATCH] queryset-refactor: Fixed a bug in the internal Query.join_map datastructure. Could result in some incorrect results(?) when using the same table multiple times in a query. git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7459 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/sql/query.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 502769b932..864563ed8e 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -673,7 +673,9 @@ class Query(object): alias_data[RHS_ALIAS] = new_alias t = self.rev_join_map[old_alias] - self.join_map[t] = new_alias + data = list(self.join_map[t]) + data[data.index(old_alias)] = new_alias + self.join_map[t] = tuple(data) self.rev_join_map[new_alias] = t del self.rev_join_map[old_alias] self.alias_refcount[new_alias] = self.alias_refcount[old_alias] @@ -778,12 +780,12 @@ class Query(object): else: lhs_table = lhs t_ident = (lhs_table, table, lhs_col, col) - alias = self.join_map.get(t_ident) - if alias and not always_create and alias not in exclusions: - self.ref_alias(alias) - if promote: - self.promote_alias(alias) - return alias + for alias in self.join_map.get(t_ident, ()): + if alias and not always_create and alias not in exclusions: + self.ref_alias(alias) + if promote: + self.promote_alias(alias) + return alias # No reuse is possible, so we need a new alias. alias, _ = self.table_alias(table, True) @@ -797,7 +799,10 @@ class Query(object): join_type = self.INNER join = (table, alias, join_type, lhs, lhs_col, col, nullable) self.alias_map[alias] = join - self.join_map[t_ident] = alias + if t_ident in self.join_map: + self.join_map[t_ident] += (alias,) + else: + self.join_map[t_ident] = (alias,) self.rev_join_map[alias] = t_ident return alias