summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2008-11-03 03:37:44 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2008-11-03 03:37:44 +0000
commit47d2576365e60af6bfebad5761d8a81324538f6d (patch)
tree23bcbafb65825a62f55873661e5b8597c45121cc
parenta5dfbeedb9f7ae148081d1dbc3e91e876526eb90 (diff)
downloadsqlalchemy-47d2576365e60af6bfebad5761d8a81324538f6d.tar.gz
- Fixed bug when using multiple query.join() with an aliased-bound
descriptor which would lose the left alias.
-rw-r--r--CHANGES3
-rw-r--r--lib/sqlalchemy/orm/query.py14
-rw-r--r--lib/sqlalchemy/orm/util.py2
-rw-r--r--test/orm/query.py14
4 files changed, 24 insertions, 9 deletions
diff --git a/CHANGES b/CHANGES
index f463ea55e..b10dbc5c8 100644
--- a/CHANGES
+++ b/CHANGES
@@ -24,6 +24,9 @@ CHANGES
when using declarative), which later would be inappropriately
aliased by Query, particularly with the various EXISTS
based comparators.
+
+ - Fixed bug when using multiple query.join() with an aliased-bound
+ descriptor which would lose the left alias.
- Improved weakref identity map memory management to no longer
require mutexing, resurrects garbage collected instance on a
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
index b358f4311..cad35972d 100644
--- a/lib/sqlalchemy/orm/query.py
+++ b/lib/sqlalchemy/orm/query.py
@@ -886,12 +886,14 @@ class Query(object):
if isinstance(onclause, sql.ClauseElement):
onclause = right_adapter.traverse(onclause)
- if prop:
- # MapperProperty based onclause
- onclause = prop
- else:
- # ClauseElement based onclause
- onclause = self._adapt_clause(onclause, False, True)
+ # TODO: is this a little hacky ?
+ if not isinstance(onclause, attributes.QueryableAttribute) or not isinstance(onclause.parententity, AliasedClass):
+ if prop:
+ # MapperProperty based onclause
+ onclause = prop
+ else:
+ # ClauseElement based onclause
+ onclause = self._adapt_clause(onclause, False, True)
clause = orm_join(clause, right_entity, onclause, isouter=outerjoin)
if alias_criterion:
diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py
index fbc1acd5d..689df8d86 100644
--- a/lib/sqlalchemy/orm/util.py
+++ b/lib/sqlalchemy/orm/util.py
@@ -349,7 +349,7 @@ class _ORMJoin(expression.Join):
adapt_from = left
else:
adapt_from = None
-
+
right_mapper, right, right_is_aliased = _entity_info(right)
if right_is_aliased:
adapt_to = right
diff --git a/test/orm/query.py b/test/orm/query.py
index ab20754df..76c926807 100644
--- a/test/orm/query.py
+++ b/test/orm/query.py
@@ -929,7 +929,17 @@ class JoinTest(QueryTest):
sess.query(User).join([('orders', orderalias), ('items', itemalias)]).filter(orderalias.user_id==9).filter(itemalias.description=='item 4').all(),
[]
)
-
+
+ def test_multiple_with_aliases(self):
+ sess = create_session()
+
+ ualias = aliased(User)
+ oalias1 = aliased(Order)
+ oalias2 = aliased(Order)
+ result = sess.query(ualias).join((oalias1, ualias.orders), (oalias2, ualias.orders)).\
+ filter(or_(oalias1.user_id==9, oalias2.user_id==7)).all()
+ self.assertEquals(result, [User(id=7,name=u'jack'), User(id=9,name=u'fred')])
+
def test_orderby_arg_bug(self):
sess = create_session()
# no arg error
@@ -1377,7 +1387,7 @@ class InstancesTest(QueryTest, AssertsCompiledSQL):
ialias = aliased(Item)
def go():
l = q.options(contains_eager(User.orders, alias=oalias), contains_eager(User.orders, Order.items, alias=ialias)).\
- outerjoin((oalias, User.orders), (ialias, Order.items)).order_by(User.id, oalias.id, ialias.id)
+ outerjoin((oalias, User.orders), (ialias, oalias.items)).order_by(User.id, oalias.id, ialias.id)
assert fixtures.user_order_result == l.all()
self.assert_sql_count(testing.db, go, 1)
sess.clear()