]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
implemented latest patch on [ticket:105], modified to support
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 20 Jul 2006 00:07:30 +0000 (00:07 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 20 Jul 2006 00:07:30 +0000 (00:07 +0000)
new ForeignKeyConstraint upon reflection

lib/sqlalchemy/databases/oracle.py

index 519f5cb65c9ecb79bdc23b9dc36efe4c6eb5d1d7..04950674aede35a462071bb44db8fcb78063ffd9 100644 (file)
@@ -95,7 +95,7 @@ AND ac.constraint_name = loc.constraint_name
 AND ac.r_owner = rem.owner(+)
 AND ac.r_constraint_name = rem.constraint_name(+)
 -- order multiple primary keys correctly
-ORDER BY ac.constraint_name, loc.position"""
+ORDER BY ac.constraint_name, loc.position, rem.position"""
 
 
 def descriptor():
@@ -221,6 +221,7 @@ class OracleDialect(ansisql.ANSIDialect):
 
        
         c = connection.execute(constraintSQL, {'table_name' : table.name.upper(), 'owner' : owner})
+        fks = {}
         while True:
             row = c.fetchone()
             if row is None:
@@ -230,13 +231,19 @@ class OracleDialect(ansisql.ANSIDialect):
             if cons_type == 'P':
                 table.c[local_column]._set_primary_key()
             elif cons_type == 'R':
-                #table.append_item(ForeignKeyConstraint(value[0], value[1], name=name))
-                table.c[local_column].append_item(
-                    schema.ForeignKey(schema.Table(remote_table,
-                                            table.metadata,
-                                            autoload=True).c[remote_column]
-                                      )
-                    )
+                try:
+                    fk = fks[cons_name]
+                except KeyError:
+                   fk = ([], [])
+                   fks[cons_name] = fk
+                refspec = ".".join([remote_table, remote_column])
+                if local_column not in fk[0]:
+                    fk[0].append(local_column)
+                if refspec not in fk[1]:
+                    fk[1].append(refspec)
+
+        for name, value in fks.iteritems():
+            table.append_item(schema.ForeignKeyConstraint(value[0], value[1], name=name))
 
     def do_executemany(self, c, statement, parameters, context=None):
         rowcount = 0