If the tables are the same in all db, you can (IMHO should) use the
same metada. There is no problem in using several metadata, or several
sessions, but I don't think that's going to help you for the secondary
databases. I think the best way in your case is to have two metadata:
one for the "accounts" and the other for the children database (one
for them all), then switch that metadata'sbind (and/or the session's
bind -- if you bind at the session level) to the correct engine on a
per request basis. Now, I don't know Pylons so I can't tell you where
to do that in a Pylons app.
To switch the metadata's bind in a request, just reassign a new engine
to the bind attribute.
Hope this helps,
--
Gaëtan de Menten
http://openhex.org
You've probably figured it out by yourself by now but well... let's
answer this anyway...
> AccountSession = elixir.session
> AccountMetadata = elixir.metadata
> AppSession = elixir.session
> AppMetadata = elixir.metadata
This won't work, as you'd expect: it'll use the same session and
metadata for both the the account stuff and the "app" stuff..
You should rather create a new session & metadata:
from sqlalchemy.orm import sessionmaker
AccountSession = elixir.session
AccountMetadata = elixir.metadata
AppSession = sessionmaker(bind=engine, autoflush=True,
transactional=True) # or whatever options you want.
AppMetadata = ThreadLocalMetaData() # I think you'd be better of with
this kind of metadata
# see http://www.sqlalchemy.org/docs/04/sqlalchemy_schema.html#docstrings_sqlalchemy.schema_ThreadLocalMetaData
> Then calling the following function to connect to the correct database
> once i've retrieved it -
>
> def database_connect_dynamic(session, metadata, dburl, **kwargs):
> import sqlalchemy
> import elixir
> engine = sqlalchemy.create_engine(dburl, **kwargs)
> session.bind = engine
> metadata.bind = engine
> from myapp.application import *
> elixir.setup_all()
>
> What i'm not sure about is if the elixir.setup_all() will work
> correctly,
No. It should only be called once. It does indeed populate the
metadata, and it also create mappers, but you can reuse the same
metadata for each of your databases. Except from that, it seems ok.
> i thought that calling this will update global mappings/
> metadata (i think!?). So i suppose i'm wondering how to "connect" my
> accounts model with one session+metadata pair and my secondary model
> with another session+metadata pair, If thats a correct way to do it?
--
The problem here is that neither of your metadata's are bound to their
engine. Your sessions are, but not the metadata. And the create_all
call doesn't go through the session (which is pretty normal).
A little tip: sessions are only for ORM stuff. For creating the tables
you don't need the ORM part.
Thanks for posting this. It would be nice if you could post this as
one (or two?) Recipe on the wiki:
http://elixir.ematia.de/trac/wiki/Recipes/