On May 19, 2011, at 5:24 PM, Faheem Mitha wrote:

> 
> Unfortunately, that is not true.  (So I guess just leaving the
> structure alone and switching dbs will not work.) There are 4 possible
> different database layouts. Also, there can be multiple schemas in
> each database.

So you have a model with a set of tables, the tables are split amongst multiple 
schemas, say schemas A, B, and C.    You then have four types of databases, 
each of which have the identical set of table designs, except the actual names 
of *just the schemas*, I.e. A, B, and C, randomly change.

That seems ridiculous.    I would absolutely name the schemas consistently.   
Or name the tables distinctly in the schemas so that search_path could be used. 
  Very unfortunate that PG doesn't support synonyms.

Since I know you aren't going for that, there's some other Python tricks all of 
which are more complex, or interfere with how mapper() works in such a way that 
I can't guarantee ongoing compatibility, than just wiping out everything with 
clear mappers and re-mapping.    I would keep the MetaData for each set of 
tables in a dictionary and pull the appropriate set of tables out for each use, 
send them into mapper().     I'd create the three copies of each Table from the 
original one using table.tometadata(schema='newschema').

# runs only once
metadatas = {
        'one':MetaData(),
        'two':MetaData(),
        'three':MetaData(),
        'four':MetaData(),
}

# runs only once, per table
def table(name, *args, **kw):
    t = Table(name, metadatas['one'], *args, **kw)
    t.tometadata(metadatas['two'], schema='two')
    t.tometadata(metadatas['three'], schema='three')
    t.tometadata(metadatas['four'], schema='four')

then to map a class:

mapper(cls, metadatas['two'].tables['some_table'])



-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to