summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2013-06-15 20:31:46 +0300
committerAnssi Kääriäinen <akaariai@gmail.com>2013-06-16 15:44:52 +0300
commitaa22cbd51aa6cb35fc658dd704948a18b27d1981 (patch)
treeb4e8c323d274b7d67a8cacf5e1972eda60390b5a /django/db/models/sql/query.py
parentb3532f7df950d07d92c231fa60a13675111208ea (diff)
Fixed #20583 -- ORM always uses setup_joins() for join generation
There were a couple of places which used Query.join() directly. By using setup_joins() in these places the code is more DRY, and in addition there is no need to directly call field.get_joining_columns() unless the field is the given join_field from get_path_info(). This makes it easier to make sure a ForeignObject subclass generates joins correctly in all cases.
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py23
1 files changed, 12 insertions, 11 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index d26b8d5c95..a56718c904 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -926,10 +926,10 @@ class Query(object):
"""
if model in seen:
return seen[model]
- int_opts = opts
chain = opts.get_base_chain(model)
if chain is None:
return alias
+ curr_opts = opts
for int_model in chain:
if int_model in seen:
return seen[int_model]
@@ -937,14 +937,14 @@ class Query(object):
# with no parents, assign the new options
# object and skip to the next base in that
# case
- if not int_opts.parents[int_model]:
- int_opts = int_model._meta
+ if not curr_opts.parents[int_model]:
+ curr_opts = int_model._meta
continue
- link_field = int_opts.get_ancestor_link(int_model)
- int_opts = int_model._meta
- connection = (alias, int_opts.db_table, link_field.get_joining_columns())
- alias = seen[int_model] = self.join(connection, nullable=False,
- join_field=link_field)
+ link_field = curr_opts.get_ancestor_link(int_model)
+ _, _, _, joins, _ = self.setup_joins(
+ [link_field.name], curr_opts, alias)
+ curr_opts = int_model._meta
+ alias = seen[int_model] = joins[-1]
return alias or seen[None]
def remove_inherited_models(self):
@@ -1321,7 +1321,7 @@ class Query(object):
return path, final_field, targets
def setup_joins(self, names, opts, alias, can_reuse=None, allow_many=True,
- allow_explicit_fk=False):
+ allow_explicit_fk=False, outer_if_first=False):
"""
Compute the necessary table joins for the passage through the fields
given in 'names'. 'opts' is the Options class for the current model
@@ -1364,8 +1364,9 @@ class Query(object):
nullable = True
connection = alias, opts.db_table, join.join_field.get_joining_columns()
reuse = can_reuse if join.m2m else None
- alias = self.join(connection, reuse=reuse,
- nullable=nullable, join_field=join.join_field)
+ alias = self.join(
+ connection, reuse=reuse, nullable=nullable, join_field=join.join_field,
+ outer_if_first=outer_if_first)
joins.append(alias)
if hasattr(final_field, 'field'):
final_field = final_field.field