module-scope. This is bad practice because you can't ensure that your
On Wed, Sep 5, 2012 at 3:44 PM, Danny Im <ior
...@gmail.com> wrote:
> Hi,
> I have an issue where if I import a sqlalchemy session and try issuing
> queries with it I'll get this error:
> UnboundExecutionError: Could not locate a bind configured on mapper
> Mapper|MyModel|models, SQL expression or this Session
> This only happens from within a package within the application, as I've put
> all of my views related files into a python package in order to better
> organize my views.
> I've tested this with view functions that reside on the 'top level' app
> directory (e.g. app/views.py) and any view functions within that file don't
> raise the above exception.
> Here are some code bits that I think are relevant:
> In /app/models.py:
> DBSession =
> scoped_session(sessionmaker(extension=ZopeTransactionExtension(),
> autoflush=False))
> def initialize_sql(engine):
> DBSession.configure(bind=engine)
> Base.metadata.bind = engine
> Base.metadata.create_all(engine)
> In /app/views.py:
> from models import DBSession, MyModel
> session = DBSession()
> session.query(MyModel)
> In /app/view_dir/stuff.py:
> import os
> import sys
> sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
> '..', '..', 'app')))
> from models import DBSession, MyModel
> session = DBSession()
> session.query(MyModel)
> I've searched for what might be causing the above error, and many results
> suggest that the session was not bound to a database. I tested this by
> issuing print statements of the DBSession, and found that the DBSession
> object is indeed different when it is imported to /app/view_dir/stuff.py,
> and the engine bound to it is not set.
> I've been able to reproduce this behavior by creating a new pyramid project
> using the sqlalchemy scaffold, creating a package to store view files, and
> creating view functions that issue queries via a sqlalchemy session object
> imported from the models.py file.
> Any ideas on why this is happening?
> Thanks!
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/pylons-discuss/-/XR91cJIkggkJ.
> To post to this group, send email to pylons-discuss@googlegroups.com.
> To unsubscribe from this group, send email to
> pylons-discuss+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.