From: Mike Bayer Date: Sat, 25 Oct 2008 18:04:59 +0000 (+0000) Subject: - When using Query.join() with an explicit clause for the X-Git-Tag: rel_0_5rc3~43 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e82eebb368718ba1fe24853373399f0fbd2c17e4;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - When using Query.join() with an explicit clause for the ON clause, the clause will be aliased in terms of the left side of the join, allowing scenarios like query(Source). from_self().join((Dest, Source.id==Dest.source_id)) to work properly. --- diff --git a/CHANGES b/CHANGES index e88175a7bc..19524ca506 100644 --- a/CHANGES +++ b/CHANGES @@ -23,6 +23,12 @@ CHANGES the "please specify primaryjoin" message when determining join condition. + - When using Query.join() with an explicit clause for the + ON clause, the clause will be aliased in terms of the left + side of the join, allowing scenarios like query(Source). + from_self().join((Dest, Source.id==Dest.source_id)) to work + properly. + - polymorphic_union() function respects the "key" of each Column if they differ from the column's name. diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index b53256a246..93699b79a6 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -885,8 +885,12 @@ class Query(object): onclause = right_adapter.traverse(onclause) 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: self._filter_aliases = right_adapter diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index 4172b99cc1..264a4d2125 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -379,7 +379,7 @@ class _ORMJoin(expression.Join): else: onclause = pj self._target_adapter = target_adapter - + expression.Join.__init__(self, left, right, onclause, isouter) def join(self, right, onclause=None, isouter=False): diff --git a/test/orm/query.py b/test/orm/query.py index 567ca317c9..db709200b3 100644 --- a/test/orm/query.py +++ b/test/orm/query.py @@ -976,6 +976,24 @@ class JoinTest(QueryTest): [('jack',)] ) + # explicit onclause with from_self(), means + # the onclause must be aliased against the query's custom + # FROM object + self.assertEquals( + sess.query(User).offset(2).from_self().join( + (Order, User.id==Order.user_id) + ).all(), + [User(name='fred')] + ) + + # same with an explicit select_from() + self.assertEquals( + sess.query(User).select_from(select([users]).offset(2).alias()).join( + (Order, User.id==Order.user_id) + ).all(), + [User(name='fred')] + ) + def test_aliased_classes(self): sess = create_session()