summaryrefslogtreecommitdiff
path: root/django/db/models/sql/compiler.py
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2013-02-21 11:12:08 +0200
committerAnssi Kääriäinen <akaariai@gmail.com>2013-02-21 12:01:23 +0200
commit3c6318e831658b88ba7c3e04f315329071b25e34 (patch)
tree8dd0fb8fbe38a726f5ddc62e4c88c0a7e975dd78 /django/db/models/sql/compiler.py
parent649118961ce3952138536235fc842921e39bfa33 (diff)
Fixed #19870 -- Regression in select_related in inheritance cases
There was a regression in case two models inherited the same parent, and one contained a foreign key to other. When select_related travelled the foreign key the other model reused the parent join made by the first model. This was likely caused by Query.join_parent_model() addition in commit 68985db48212c701a3d975636123a5d79bdc006f. Thanks to Trac alias loic84 for report & tests.
Diffstat (limited to 'django/db/models/sql/compiler.py')
-rw-r--r--django/db/models/sql/compiler.py13
1 files changed, 9 insertions, 4 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 977eb1afa2..22d025ecb2 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -265,14 +265,19 @@ class SQLCompiler(object):
qn2 = self.connection.ops.quote_name
aliases = set()
only_load = self.deferred_to_columns()
- seen = self.query.included_inherited_models.copy()
- if start_alias:
- seen[None] = start_alias
+ if not start_alias:
+ start_alias = self.query.get_initial_alias()
+ # The 'seen_models' is used to optimize checking the needed parent
+ # alias for a given field. This also includes None -> start_alias to
+ # be used by local fields.
+ seen_models = {None: start_alias}
+
for field, model in opts.get_fields_with_model():
if from_parent and model is not None and issubclass(from_parent, model):
# Avoid loading data for already loaded parents.
continue
- alias = self.query.join_parent_model(opts, model, start_alias, seen)
+ alias = self.query.join_parent_model(opts, model, start_alias,
+ seen_models)
table = self.query.alias_map[alias].table_name
if table in only_load and field.column not in only_load[table]:
continue