From: Mike Bayer Date: Thu, 16 Feb 2006 06:13:16 +0000 (+0000) Subject: Join object wasnt exporting foreign keys correctly X-Git-Tag: rel_0_1_1~42 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=52396072d0971086321d2908575a2e6b7d067ba3;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Join object wasnt exporting foreign keys correctly 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 --- diff --git a/lib/sqlalchemy/mapping/properties.py b/lib/sqlalchemy/mapping/properties.py index 6edb157420..f686e588c4 100644 --- a/lib/sqlalchemy/mapping/properties.py +++ b/lib/sqlalchemy/mapping/properties.py @@ -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) diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index c99af4c6d0..293880bf3d 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -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 = []