On Jun 30, 2010, at 2:08 PM, Richard Kuesters wrote:

> hi all,
> 
> i was wondering if its is possible to map one single class (let's say, 
> Person) against multiple schemas (taking mysql, for example) with the same 
> table structure.
> 
> i know it can be done with sharding, but what troubles me is the fact that 
> one schema will have the app data, and the other schemas users data (and FKs 
> for some relationships).
> 
> can it be done?
> 
> thanks a lot,
> richard.

you have to make a choice how you'd like to differentiate between the two 
schemas.  

If you'd like to do something like this:

obj1 = Obj1(schema='foo')
obj2 = Obj1(schema='bar')

and then obj1/obj2 stay in those schemas all while using one 
session/connection, that's one pattern.  

If OTOH you're looking to treat the two schemas in a fluid style, where one 
object can move to the other:

obj1 = Session1.query(Obj1).first()

obj1 = Session2.merge(obj1)

Session2.commit()

that's another pattern.


The first pattern would use "entity name", which is in the FAQ at 
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/EntityName .   You can make a 
copy of a Table object from schema1 to schema2 using :

t2 = t1.tometadata(t1.metadata, schema='new_schema')

or you can use two different MetaData objects, doesn't matter much.

The second pattern is pretty obvious, use two Sessions and bind them to 
different engines that point to each schema.  Or use one Session and change the 
"bind" attribute to point to each different schema as needed (you'l want to 
close() it first before changing "bind"), if your app works that way.

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

Reply via email to