title: Attaching a pre-built query to a scoped_session in SQLAlchemy
body: <p>I'm receiving a weird error, <code>AttributeError: 'scoped_session' object has no attribute '_autoflush'</code>, when attempting to execute a pre-built query on a scoped_session in the SQLAlchemy ORM.</p>
  
  <p>I'm using a context manager to yield a scoped_session,</p>
  
  <pre><code>@contextmanager
  def connect(my_session_factory):
      session = scoped_session(my_session_factory)
      try:
          yield session
      except Exception as exception:
          session.rollback()
          raise exception
      finally:
          session.close()
  </code></pre>
  
  <p>and then am using it like so:</p>
  
  <pre><code>from sqlalchemy.orm import Query
  
  query = Query(my_model).offset(my_offset).limit(my_limit)
  with connect(my_session_factory) as session:
      instances = query.with_session(session).all()
      return instances
  </code></pre>
  
  <p>This, however, raises the exception above.</p>
  
  <p>I note also that queries of the form <code>session.query(my_model)</code> work just fine.</p>
  
  <p>Where am I going wrong? Many thanks!</p>