]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- fix to select_by(<propname>=<object instance>) -style joins in conjunction
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 17 May 2007 15:11:34 +0000 (15:11 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 17 May 2007 15:11:34 +0000 (15:11 +0000)
with many-to-many relationships, bug introduced in r2556
- the "reverse_direction" flag in _create_lazy_clause works correctly for a many-to-many
relationship (i.e. the reverse is on which clause, not which column in the clause, in the
case of m2m)

CHANGES
lib/sqlalchemy/orm/properties.py
lib/sqlalchemy/orm/strategies.py
test/orm/mapper.py

diff --git a/CHANGES b/CHANGES
index 277f57e2f076c4ad408100152d6c5102a33a1e1e..332611431ae354d8e8fb902f35b1f33650b70e72 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -28,6 +28,8 @@
     - session.get() and session.load() propigate **kwargs through to query
     - fix to polymorphic query which allows the original polymorphic_union
       to be embedded into a correlated subquery [ticket:577]
+    - fix to select_by(<propname>=<object instance>) -style joins in conjunction
+      with many-to-many relationships, bug introduced in r2556 
 - mysql
     - support for column-level CHARACTER SET and COLLATE declarations,
       as well as ASCII, UNICODE, NATIONAL and BINARY shorthand.
index 171021da824b53733636966aac42566c507471ae..6677e4ab94628afb92a7351e408a120732cee0ec 100644 (file)
@@ -418,6 +418,8 @@ class PropertyLoader(StrategizedProperty):
         if logging.is_info_enabled(self.logger):
             self.logger.info(str(self) + " setup primary join " + str(self.primaryjoin))
             self.logger.info(str(self) + " setup polymorphic primary join " + str(self.polymorphic_primaryjoin))
+            self.logger.info(str(self) + " setup secondary join " + str(self.secondaryjoin))
+            self.logger.info(str(self) + " setup polymorphic secondary join " + str(self.polymorphic_secondaryjoin))
             self.logger.info(str(self) + " foreign keys " + str([str(c) for c in self.foreign_keys]))
             self.logger.info(str(self) + " remote columns " + str([str(c) for c in self.remote_side]))
             self.logger.info(str(self) + " relation direction " + (self.direction is sync.ONETOMANY and "one-to-many" or (self.direction is sync.MANYTOONE and "many-to-one" or "many-to-many")))
index 033d0cac1415cdcb5dadcc43adcbb303baa6b987..f1b159318f374fa5567842fed5e22c82a5e1c3b4 100644 (file)
@@ -253,7 +253,7 @@ class LazyLoader(AbstractRelationLoader):
         reverse = {}
 
         def should_bind(targetcol, othercol):
-            if reverse_direction:
+            if reverse_direction and not secondaryjoin:
                 return targetcol in remote_side
             else:
                 return othercol in remote_side
@@ -293,10 +293,14 @@ class LazyLoader(AbstractRelationLoader):
 
         lazywhere = primaryjoin.copy_container()
         li = mapperutil.BinaryVisitor(visit_binary)
-        li.traverse(lazywhere)
+        
+        if not secondaryjoin or not reverse_direction:
+            li.traverse(lazywhere)
         
         if secondaryjoin is not None:
             secondaryjoin = secondaryjoin.copy_container()
+            if reverse_direction:
+                li.traverse(secondaryjoin)
             lazywhere = sql.and_(lazywhere, secondaryjoin)
  
         if hasattr(cls, 'parent_property'):
index a9cc40102918e487f9b84220a36a2400cf25ff04..4ce1be2fa762aeb56d15784137a979787d86f8ef 100644 (file)
@@ -346,8 +346,8 @@ class MapperTest(MapperSuperTest):
         self.assert_result(k, Keyword, *item_keyword_result[1]['keywords'][1])
 
         
-    def testjoinvia(self):
-        """test the join_via and join_to functions"""
+    def testautojoin(self):
+        """test functions derived from Query's _join_to function."""
         
         m = mapper(User, users, properties={
             'orders':relation(mapper(Order, orders, properties={
@@ -386,10 +386,10 @@ class MapperTest(MapperSuperTest):
             assert False
         except AttributeError:
             assert True
-    
         
-    def testjoinviam2m(self):
-        """test the join_via and join_to functions"""
+    def testautojoinm2m(self):
+        """test functions derived from Query's _join_to function."""
+        
         m = mapper(Order, orders, properties = {
             'items' : relation(mapper(Item, orderitems, properties = {
                 'keywords' : relation(mapper(Keyword, keywords), itemkeywords)
@@ -402,10 +402,15 @@ class MapperTest(MapperSuperTest):
         l = q.filter(keywords.c.name=='square').join(['items', 'keywords']).list()
         self.assert_result(l, Order, order_result[1])
 
+        # test comparing to an object instance
+        item = sess.query(Item).selectfirst()
+        l = sess.query(Item).select_by(keywords=item.keywords[0])
+        assert item == l[0]
         
     def testcustomjoin(self):
         """test that the from_obj parameter to query.select() can be used
         to totally replace the FROM parameters of the generated query."""
+
         m = mapper(User, users, properties={
             'orders':relation(mapper(Order, orders, properties={
                 'items':relation(mapper(Item, orderitems))