This is the way to go, but isn't Josh doing it already?
The Session class takes care of creating session objects as necessary
and setting their bind attribute. There may be a problem of an
existing session object when Session.configure is called, but you
should be calling initialize() at the very beginning of the
application in environment.py, before the middleware stack has been
set up.
The Pylons SQLAlchemy template used to set Session to None initially,
but some users complained that they couldn't import Session directly
in that case, they had to import meta.session. The Session.configure
solution is recommended by SQLAlchemy's author Mike Bayer, so that's
why Pylons is using it now.
--
Mike Orr <slugg...@gmail.com>
Ah, ouch.
Well, the templates that come with Pylons 1.0 avoid reassigning any
global names in model.meta. Instead, model/__init__.py looks like this:
from (project).model.meta import Session, Base
def init_model(engine):
"""Call me before using any of the tables or classes in the model"""
Session.configure(bind=engine)
(Incidentally, it would be useful to have a sample project created using
the latest templates on the web somewhere. Say, github; updated
automatically whenever you change the template, so that, for example,
people could see the differences between a "standard" Pylons 0.97
project and a Pylons 1.0 one.)
> I guess I'm confused now as to why meta.Session is set up the way it
> is. This approach seems like it can introduce subtle bugs; what's the
> best way to avoid bugs like this in the future?
Do the same thing as above: avoid assignments to globals.
> Specific questions:
>
> Should I update the wiki.pylonshq.com article to use meta.Session
> instead of importing Session directly?
That might be safer, for people who have older projects with the unsafe
init_model. However the new convention seems to be from foo.model.meta
import Session.
Marius Gedminas
--
Cheap, Fast, Good -- pick two.
If the article is not using Session.configure, it should be changed.
But it doesn't matter if it imports meta or Session. You can add a
note saying it's valid both ways.
--
Mike Orr <slugg...@gmail.com>