[Chicago] help from Django and Pylons developers

Kumar McMillan kumar.mcmillan at gmail.com
Mon Apr 28 22:27:03 CEST 2008


On Mon, Apr 28, 2008 at 3:15 PM, Kumar McMillan
<kumar.mcmillan at gmail.com> wrote:
> On Mon, Apr 28, 2008 at 1:18 PM, Ian Bicking <ianb at colorstudy.com> wrote:
>  > > I have never a Pylons example for connecting to multiple databases. Would
>  > you mind posting an example?
>  > >
>
>  I can't find this documented anywhere but if you configure a Pylons
>  app with this structure (using SQLAlchemy 0.4),
http://wiki.pylonshq.com/display/pylonsdocs/Using+SQLAlchemy+with+Pylons

duh, an example of multiple databases is actually at the bottom of
that same page! ;)

They suggest more or less the same thing but it's a little more
concise and the session construction is more sane ...

[app:main]
sqlalchemy.default.url = "mysql://..."
sqlalchemy.default.pool_recycle = 3600
sqlalchemy.log.url = "sqlite://..."

default_engine = engine_from_config(config, 'sqlalchemy.default.')
log_engine = engine_from_config(config, 'sqlalchemy.log.')
init_model(default_engine, log_engine)

...and so on


>
>  ... then, if you wanted to, say, connect to a read-only db and a
>  write-only db, you could update your config like this:
>
>  [app:main]
>  # ...
>  read_db.url = postgres://read_user:pw@readhost/read_db
>  #read_db.debug = True
>  write_db.url = postgres://write_user:pw@writehost/write_db
>
>  ... and you could change environment.py to call init_model() like:
>
>  def load_environment(global_conf, app_conf):
>     # ...
>     read_engine = engine_from_config(config, 'read_db.')
>     write_engine = engine_from_config(config, 'write_db.')
>     init_model(read_engine, write_engine)
>
>  ... and update the init_model() def in model/__init__.py to :
>
>  from sqlalchemy import orm
>  from myapp.model import meta
>
>  def init_model(read_engine, write_engine):
>     ReadSession = orm.scoped_session(orm.sessionmaker(autoflush=False,
>  transactional=False, bind=read_engine))
>     meta.read_metadata.bind = read_engine
>     meta.read_session = ReadSession()
>     # setup read mappers here..
>
>     WriteSession = orm.scoped_session(orm.sessionmaker(autoflush=True,
>  transactional=True, bind=write_engine))
>     meta.write_metadata.bind = write_engine
>     meta.write_session = WriteSession()
>     # setup write mappers here..
>
>
>  In other words, the config file is just a place to store values, any
>  values you want, and the environment.py module gives you the chance to
>  customize the initialization of your app any way you want.  You store
>  the initialization code wherever you want, Pylons doesn't force any
>  conventions on you other than by creating a default module layout when
>  you start your project.
>
>  Kumar
>


More information about the Chicago mailing list