Unexecuted import-time Session.query() thwarts Session.configure(bind=x)

Issue #1924 resolved
Former user created an issue

Thankfully, it's very easy to not do the below. But when it's going on it is very unclear why the error has occurred.

{{{
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

S = scoped_session(sessionmaker())
q = S.query(sqlalchemy.func.max())

def test_session():
    the_engine = create_engine('sqlite://')
    S.configure(bind=the_engine)
    S.execute("SELECT 1")

Comments (4)

  1. Mike Bayer repo owner

    what logic would you have SQLAlchemy apply here? Most blunt would be, configure() raises an error once any session has been created. I'm not sure what other options exist besides that. I can see some users having an issue with such a behavior.

  2. Former user Account Deleted

    I'm not sure what it should do, but if I change the code to explicitly create a session,

    Maybe it could be a warning? My code is a multi-threaded web application so when not in testing a new scoped_session comes into play. At the console S.execute() gives the 'no bind' error but in the normal running application S.execute("SELECT 1") works despite import-time Query brokenness.
    
    {{{
    import sqlalchemy
    from sqlalchemy import create_engine
    from sqlalchemy.orm import scoped_session, sessionmaker
    
    S = scoped_session(sessionmaker())
    q = S.query(sqlalchemy.func.max())
    
    def test_session():
        the_engine = create_engine('sqlite://')
        s = S(bind=the_engine)
        s.execute("SELECT 1")
    
  3. Log in to comment