3 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: working with multipl...
FromSent OnAttachments
Max Ischenko23 Jan 2007 07:35 
Michael Bayer28 Jan 2007 20:39 
Rick Morrison28 Jan 2007 21:17 
Subject:[sqlalchemy] Re: working with multiple databases
From:Michael Bayer (zzz@gmail.com)
Date:01/28/2007 08:39:37 PM
List:com.googlegroups.sqlalchemy

On Jan 23, 10:36 am, "Max Ischenko" <isch@gmail.com> wrote:

As far as I understand, I need a session per database. In Pylons, I get it "for free" via session_context binding. In other words, I have setup a session_context object for each of the database I need to work with.

youd need a separate engine for each database. since different connection strings and all that. A single Session can in fact talk to multiple databases at once, so im not sure if multiple sessions/ sessioncontexts are really required (however, i can see how it may be the most intuitive approach to just use separate sessions).

Do I need to use separate DynamicMetaData() for each db conn? Do I need to connect single metadata to the correct session? SA docs shows how to connect metadata to a engine but all I have is a session.

OK i think talking to multiple DB's is a little involved and there is currently no official "pattern" to this. it also depends on how your multiple DB's relate to your classes. for example, if you have certain classes that are stored in certain DB's, you can map quite easily using just regular BoundMetaData instances:

metadata1 = BoundMetaData('foo://database1') metadata2 = BoundMetaData('foo://database2')

table1 = Table('table1', metadata1, autoload=True) table2 = Table('table2', metadata2, autoload=True)

mapper(ClassA, table1) mapper(ClassB, table2)

...and youre done. You can use just one Session that has ClassA and ClassB in it at the same time. So, we have a lot of possibilities here, but app configs and database configs can vary greatly so it may be a little bit overwhelmingly open ended.