It's not too bad, but you have to do your own custom app setup, and
modify your init_model stuff to create multiple transaction manager.
app_cfg.py:
from webapp.model import init_model
from sqlalchemy import engine_from_config
from tg.configuration import AppConfig, Bunch
from pylons import config as pylons_config
class MyAppConfig(AppConfig):
def setup_sqlalchemy(self):
"""Setup SQLAlchemy database engine"""
users_engine = engine_from_config(pylons_config,
'sqlalchemy_users.')
samples_engine = engine_from_config(pylons_config,
'sqlalchemy_samples.')
config['pylons.app_globals'].sa_engine = users_engine
config['pylons.app_globals'].sa_users_engine = users_engine
config['pylons.app_globals'].sa_samples_engine =
samples_engine
# Pass the engine to initmodel, to be able to introspect
tables
init_model(users_engine, samples_engine)
base_config = MyAppConfig()
...
model/__init__.py:
import model.users
import model.samples
from model.users.mappers import *
from model.samples.mappers import *
from zope.sqlalchemy import ZopeTransactionExtension
from sqlalchemy.orm import scoped_session, sessionmaker
# Global session manager. DBSession() returns the session object
# appropriate for the current web request.
maker = sessionmaker(autoflush=True, autocommit=False,
extension=ZopeTransactionExtension())
DBSession = UsersDBSession = scoped_session(maker)
maker3 = sessionmaker(autoflush=True, autocommit=False,
extension=ZopeTransactionExtension())
SamplesDBSession = scoped_session(maker3)
from model.users.metadata import metadata as users_metadata
from model.samples.metadata import metadata as samples_metadata
def init_model(users_engine, samples_engine):
"""Call me before using any of the tables or classes in the
model."""
global UsersDBSession, SamplesDBSession, users_metadata,
samples_metadata
UsersDBSession.configure(bind=users_engine)
SamplesDBSession.configure(bind=samples_engine)
users_metadata.bind = users_engine
samples_metadata.bind = samples_engine
This is sort of an off-the cuff answer, we will provide a better one
in the next documentation release.
Come find me on IRC if you need some more help.
cheers.
-chris
> According to Mark Ramm's blog, supporting multiple databases is
> supposed to be easy (seehttp://
compoundthinking.com/blog/index.php/2008/07/31/10-reasons-why-...).