#NEWLINE# @ reflection . cache #NEWLINE# @ _db_plus_owner #NEWLINE# def get_foreign_keys( self , connection , tablename , dbname , owner , schema , **kw ) : #NEWLINE# #INDENT# RR = ischema . ref_constraints #NEWLINE# C = ischema . key_constraints . alias ( 'C' ) #NEWLINE# R = ischema . key_constraints . alias ( 'R' ) #NEWLINE# s = sql . select ( [ C . c . column_name , R . c . table_schema , R . c . table_name , R . c . column_name , RR . c . constraint_name , RR . c . match_option , RR . c . update_rule , RR . c . delete_rule ] , sql . and_ ( ( C . c . table_name == tablename ) , ( C . c . table_schema == owner ) , ( C . c . constraint_name == RR . c . constraint_name ) , ( R . c . constraint_name == RR . c . unique_constraint_name ) , ( C . c . ordinal_position == R . c . ordinal_position ) ) , order_by = [ RR . c . constraint_name , R . c . ordinal_position ] ) #NEWLINE# fkeys = [ ] #NEWLINE# ( fknm , scols , rcols ) = ( None , [ ] , [ ] ) #NEWLINE# def fkey_rec( ) : #NEWLINE# #INDENT# return { #NEWLINE# #INDENT# 'name' : None , #NEWLINE# 'constrained_columns' : [ ] , #NEWLINE# 'referred_schema' : None , #NEWLINE# 'referred_table' : None , #NEWLINE# 'referred_columns' : [ ] , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# fkeys = util . defaultdict ( fkey_rec ) #NEWLINE# for r in connection . execute ( s ) . fetchall ( ) : #NEWLINE# #INDENT# ( scol , rschema , rtbl , rcol , rfknm , fkmatch , fkuprule , fkdelrule ) = r #NEWLINE# rec = fkeys [ rfknm ] #NEWLINE# rec [ 'name' ] = rfknm #NEWLINE# if ( not rec [ 'referred_table' ] ) : #NEWLINE# #INDENT# rec [ 'referred_table' ] = rtbl #NEWLINE# if ( ( schema is not None ) or ( owner != rschema ) ) : #NEWLINE# #INDENT# if dbname : #NEWLINE# #INDENT# rschema = ( ( dbname + '.' ) + rschema ) #NEWLINE# #UNINDENT# rec [ 'referred_schema' ] = rschema #NEWLINE# #UNINDENT# #UNINDENT# ( local_cols , remote_cols ) = ( rec [ 'constrained_columns' ] , rec [ 'referred_columns' ] ) #NEWLINE# local_cols . append ( scol ) #NEWLINE# remote_cols . append ( rcol ) #NEWLINE# #UNINDENT# return list ( fkeys . values ( ) )