]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Join object wasnt exporting foreign keys correctly
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 16 Feb 2006 06:13:16 +0000 (06:13 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 16 Feb 2006 06:13:16 +0000 (06:13 +0000)
compile_synchronizers in PropertyLoader needed to take into account the full list of tables for each mapper when looking for synchronization rules, not just primary table

lib/sqlalchemy/mapping/properties.py
lib/sqlalchemy/sql.py

index 6edb157420ff729f9a3c71233d3f67321505790c..f686e588c453d9fd5525c110374f14658be02c30 100644 (file)
@@ -523,12 +523,23 @@ class PropertyLoader(MapperProperty):
 
         SyncRule = PropertyLoader.SyncRule
 
+        parent_tables = util.HashSet(self.parent.tables + [self.parent.primarytable])
+        target_tables = util.HashSet(self.mapper.tables + [self.mapper.primarytable])
+
+        def check_for_table(binary, l):
+            for col in [binary.left, binary.right]:
+                if col.table in l:
+                    return col
+            else:
+                return None
+        
         def compile(binary):
             """assembles a SyncRule given a single binary condition"""
             if binary.operator != '=' or not isinstance(binary.left, schema.Column) or not isinstance(binary.right, schema.Column):
                 return
 
             if binary.left.table == binary.right.table:
+                # self-cyclical relation
                 if binary.left.primary_key:
                     source = binary.left
                     dest = binary.right
@@ -544,18 +555,23 @@ class PropertyLoader(MapperProperty):
                 else:
                     raise "assert failed"
             else:
-                colmap = {binary.left.table : binary.left, binary.right.table : binary.right}
-                if colmap.has_key(self.parent.primarytable) and colmap.has_key(self.target):
+                pt = check_for_table(binary, parent_tables)
+                tt = check_for_table(binary, target_tables)
+                st = check_for_table(binary, [self.secondary])
+                #print "parenttable", [t.name for t in parent_tables]
+                #print "ttable", [t.name for t in target_tables]
+                #print "OK", str(binary), pt, tt, st
+                if pt and tt:
                     if self.direction == PropertyLoader.ONETOMANY:
-                        self.syncrules.append(SyncRule(self.parent, colmap[self.parent.primarytable], colmap[self.target], dest_mapper=self.mapper))
+                        self.syncrules.append(SyncRule(self.parent, pt, tt, dest_mapper=self.mapper))
                     elif self.direction == PropertyLoader.MANYTOONE:
-                        self.syncrules.append(SyncRule(self.mapper, colmap[self.target], colmap[self.parent.primarytable], dest_mapper=self.parent))
+                        self.syncrules.append(SyncRule(self.mapper, tt, pt, dest_mapper=self.parent))
                     else:
                         raise "assert failed"
-                elif colmap.has_key(self.parent.primarytable) and colmap.has_key(self.secondary):
-                    self.syncrules.append(SyncRule(self.parent, colmap[self.parent.primarytable],  colmap[self.secondary], direction=PropertyLoader.ONETOMANY))
-                elif colmap.has_key(self.target) and colmap.has_key(self.secondary):
-                    self.syncrules.append(SyncRule(self.mapper, colmap[self.target], colmap[self.secondary], direction=PropertyLoader.MANYTOONE))
+                elif pt and st:
+                    self.syncrules.append(SyncRule(self.parent, pt, st, direction=PropertyLoader.ONETOMANY))
+                elif tt and st:
+                    self.syncrules.append(SyncRule(self.mapper, tt, st, direction=PropertyLoader.MANYTOONE))
 
         self.syncrules = []
         processor = BinaryVisitor(compile)
index c99af4c6d0fc5788ca48e7d1a1d51a7c8efb0d3e..293880bf3d3f7245e3ab0c8915adbb1b44864441 100644 (file)
@@ -595,7 +595,7 @@ class FromClause(Selectable):
                     cp = self._proxy_column(co)
                     self._orig_cols[co.original] = cp
     def _exportable_columns(self):
-       return []
+        return []
     def _proxy_column(self, column):
         return column._make_proxy(self)
     
@@ -843,7 +843,7 @@ class Join(FromClause):
         if column.primary_key:
             self._primary_key.append(column)
         if column.foreign_key:
-            self._foreign_keys.append(column)
+            self._foreign_keys.append(column.foreign_key)
         return column
     def _match_primaries(self, primary, secondary):
         crit = []