]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- When using Query.join() with an explicit clause for the
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 25 Oct 2008 18:04:59 +0000 (18:04 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 25 Oct 2008 18:04:59 +0000 (18:04 +0000)
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.

CHANGES
lib/sqlalchemy/orm/query.py
lib/sqlalchemy/orm/util.py
test/orm/query.py

diff --git a/CHANGES b/CHANGES
index e88175a7bcc12ba04978f51b3efae8898e4894bb..19524ca50643d016b0357768f7af5c42b4f7a588 100644 (file)
--- 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.
 
index b53256a246e818ff7baaec5ce5ae7c416834d38f..93699b79a6ca996885624a9853740517fd7fea12 100644 (file)
@@ -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
index 4172b99cc1fb7fa9c4d052dafa9155410ddaa6a6..264a4d2125d1b84592f04c9b441d2011406d290f 100644 (file)
@@ -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):
index 567ca317c9b639ea0b027734f64f6f4802b409e7..db709200b3426a3e6b3940eb6a0f7154b70c1932 100644 (file)
@@ -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()