summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-06-04 18:23:06 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2013-06-04 18:23:06 -0400
commit11578ba709ff3e5f2a2a2a9f92bf6fdc2ee6d328 (patch)
tree4202758c520dd62e85f18dc37da60d5774d375c8 /lib/sqlalchemy/orm
parent58e917fe2de2f4b643f3bf2468f51aef27e67747 (diff)
downloadsqlalchemy-11578ba709ff3e5f2a2a2a9f92bf6fdc2ee6d328.tar.gz
- improve overlapping selectables, apply to both query and relationship
- clean up inspect() calls within query._join() - make sure join.alias(flat) propagates - fix almost all assertion tests
Diffstat (limited to 'lib/sqlalchemy/orm')
-rw-r--r--lib/sqlalchemy/orm/query.py32
-rw-r--r--lib/sqlalchemy/orm/relationships.py8
2 files changed, 17 insertions, 23 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
index 8ab81cb13..39ed8d8bf 100644
--- a/lib/sqlalchemy/orm/query.py
+++ b/lib/sqlalchemy/orm/query.py
@@ -1797,14 +1797,6 @@ class Query(object):
right_entity, onclause,
outerjoin, create_aliases, prop)
- def _tables_overlap(self, left, right):
- """Return True if parent/child tables have some overlap."""
-
- return bool(
- set(sql_util.find_tables(left)).intersection(
- sql_util.find_tables(right)
- )
- )
def _join_left_to_right(self, left, right,
onclause, outerjoin, create_aliases, prop):
@@ -1825,14 +1817,19 @@ class Query(object):
"are the same entity" %
(left, right))
- # TODO: get the l_info, r_info passed into
- # the methods so inspect() doesnt need to be called again
l_info = inspect(left)
r_info = inspect(right)
- overlap = self._tables_overlap(l_info.selectable, r_info.selectable)
+
+ overlap = not create_aliases and \
+ sql_util.selectables_overlap(l_info.selectable,
+ r_info.selectable)
+ if overlap and l_info.selectable is r_info.selectable:
+ raise sa_exc.InvalidRequestError(
+ "Can't join table/selectable '%s' to itself" %
+ l_info.selectable)
right, onclause = self._prepare_right_side(
- right, onclause,
+ r_info, right, onclause,
create_aliases,
prop, overlap)
@@ -1846,10 +1843,11 @@ class Query(object):
else:
self._joinpoint = {'_joinpoint_entity': right}
- self._join_to_left(left, right, onclause, outerjoin)
+ self._join_to_left(l_info, left, right, onclause, outerjoin)
- def _prepare_right_side(self, right, onclause, create_aliases, prop, overlap):
- info = inspect(right)
+ def _prepare_right_side(self, r_info, right, onclause, create_aliases,
+ prop, overlap):
+ info = r_info
right_mapper, right_selectable, right_is_aliased = \
getattr(info, 'mapper', None), \
@@ -1931,8 +1929,8 @@ class Query(object):
return right, onclause
- def _join_to_left(self, left, right, onclause, outerjoin):
- info = inspect(left)
+ def _join_to_left(self, l_info, left, right, onclause, outerjoin):
+ info = l_info
left_mapper = getattr(info, 'mapper', None)
left_selectable = info.selectable
diff --git a/lib/sqlalchemy/orm/relationships.py b/lib/sqlalchemy/orm/relationships.py
index 95fa28613..33377d3ec 100644
--- a/lib/sqlalchemy/orm/relationships.py
+++ b/lib/sqlalchemy/orm/relationships.py
@@ -17,7 +17,7 @@ from .. import sql, util, exc as sa_exc, schema
from ..sql.util import (
ClauseAdapter,
join_condition, _shallow_annotate, visit_binary_product,
- _deep_deannotate, find_tables
+ _deep_deannotate, find_tables, selectables_overlap
)
from ..sql import operators, expression, visitors
from .interfaces import MANYTOMANY, MANYTOONE, ONETOMANY
@@ -404,11 +404,7 @@ class JoinCondition(object):
def _tables_overlap(self):
"""Return True if parent/child tables have some overlap."""
- return bool(
- set(find_tables(self.parent_selectable)).intersection(
- find_tables(self.child_selectable)
- )
- )
+ return selectables_overlap(self.parent_selectable, self.child_selectable)
def _annotate_remote(self):
"""Annotate the primaryjoin and secondaryjoin