Hi All,

I'm trying to convert some code I wrote over to using SA.  I have code 
that generates an HTML given the name of a table, it parses the 
information_schema to get the structure of the table, then creates an 
SQL query to get the data and formats the table with the column names at 
the top and one row per <TR>.  The simple part of that I've switched 
over to using SA metadata, but now I'm looking to take the next step and 
dereference any foreign keys. 

Because of how I set up my schema, any table that is referenced via a 
foreign key will have a PK column named "id" and a varchar called "name" 
that I want to display in place of the FK column in the referencing table.

Will something in SA build this SQL for me?  Right now I'm playing with 
a loop like this:

def dereferenced_columns( self ):
    columnnames = []
    derefcolumnstext = ''
    for column in self.table.columns:
        if column.foreign_key:
            # do whatever it takes to figure out the foreign key
            # and add it to my list
        else:
            columnnames.append( "%s" % column )
    return derefcolumnstext += ','.join( columnnames )


And I'll end up doing something similar with the table clause to create 
deterministic aliases, I can't actually use the table names as they 
appear in the "table.foreignkey.column" since some tables have FKs to 
themselves and some tables need to be referenced multiple times.

I'm new to SA and unfamiliar with all the functionality, most examples 
assume that you're creating the schema and have implicit knowledge of 
it, rather than trying to read it from the metadata, so I'm doing a lot 
of iteration and dir()'s over columns objects in the metadata and it's 
slow going.  Anyone see a simplification to what I'm doing?  I can't 
help feeling like I'm making this harder than it should be.

Thanks,
e.


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to