Re: New SQLAlchemy session created when referenced in views package

68 views
Skip to first unread message

Jonathan Vanasco

unread,
Sep 5, 2012, 8:50:21 PM9/5/12
to pylons-...@googlegroups.com

scoped_session returns a session, so to me i see:

/app/models.py:
   creates DBSession instance via scoped_session
   initialize_sql initializes it

/app/views / etc :
   imports the DBSession from /app/models, then calls it?  i think that's where an issue may be.

have you looked into the SqlAHelper package ? https://github.com/Pylons/SQLAHelper

it's in a freeze mode, and abandoned, but it still works.



Michael Merickel

unread,
Sep 5, 2012, 8:52:31 PM9/5/12
to pylons-...@googlegroups.com
It appears from your example code as if you're using the session from
module-scope. This is bad practice because you can't ensure that your
initialize_sql() function has executed before that code is run (when
the module is imported).

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-...@googlegroups.com.
> To unsubscribe from this group, send email to
> pylons-discus...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.
Reply all
Reply to author
Forward
0 new messages