grassoalvaro,
It's completely unnecessary to maintain a shared threadlocal DBSession as a
module-level object. Delete it from your code and stop referencing it from
your views. You will find it is much easier to test and extend your code
without it.
What I do instead is attach the SQLAlchemy Session for the current request
as request.db in a NewRequest listener (an event called right after Pyramid
creates a new Request object). A view method that needs the request
references it as request.db. Any other database-using function simply
accepts the session as an argument, or if you have an attached mapped object
you can access its session as sqlalchemy.orm.Session.object_session(obj).
If you are willing to define foreign keys between packages in a disciplined
DAG by using class properties instead of strings:
referenced_user_id = Column(Integer, ForeignKey(User.user_id), index=True)
instead of
referenced_user_id = Column(Interger, ForeignKey('User.user_id'), index=True)
then it is also totally unnecessary to have a shared declarative_base(). I like
this because it makes it easy to call
somepackage.Base.metadata.create_all(bind=some_engine) to create only the
tables belonging to a
particular package.
As an aside, you don't have to bind your metadata to a particular database
engine, either. SQLAlchemy works
great without any of those outdated patterns.
Daniel
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.