[Chicago] help from Django and Pylons developers

Kumar McMillan kumar.mcmillan at gmail.com
Mon Apr 28 22:56:46 CEST 2008


On Mon, Apr 28, 2008 at 3:39 PM, Massimo Di Pierro
<mdipierro at cs.depaul.edu> wrote:
> How would you handle the case when the two databases have a table with the
> same name?
>  Is it a problem?

shouldn't be a problem.  You are creating two separate engine objects.
 If you mapped each of your classes to a single table in a specific
engine (a typical use case) and the same table existed in both engines
then there would be no conflict.  However, it is possible to map a
single class to multiple tables across engines, in which case you'd
have to be careful when the same table exists in both engines.

SQLAlchemy generally keeps everything separate like this: you have a
metadata object, an engine object, a session object, table objects,
and classes that maps to whatever you want -- these are the interfaces
to the data.  This both makes it great (very flexible) and also
annoying (hard to learn, requires a lot of code for simple tasks,
things often go wrong because you probably just forgot to set
something up, etc).

Luckily, for those who wish to "just get sh*t done", there is Elixir:
http://elixir.ematia.de/trac/wiki
:)

>
>  Massimo
>
>
>
>
>  On Apr 28, 2008, at 3:27 PM, Kumar McMillan wrote:
>
>
> >
> >
> >
> > 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
> > >
> > >
> >
> > _______________________________________________
> > Chicago mailing list
> > Chicago at python.org
> > http://mail.python.org/mailman/listinfo/chicago
> >
>
>
>  _______________________________________________
>  Chicago mailing list
>  Chicago at python.org
>  http://mail.python.org/mailman/listinfo/chicago
>


More information about the Chicago mailing list