flask-sqlalchemy icon indicating copy to clipboard operation
flask-sqlalchemy copied to clipboard

Binds parameter not respected

Open matt8754 opened this issue 8 years ago • 3 comments

#345 is closed without a fix. Now I cannot specify an empty dictionary for the binds parameter in order to use the technique as describe by the SQLAlchemy docs regarding joining a session into an external transaction.

matt8754 avatar Feb 27 '17 10:02 matt8754

It was closed with a fix, there's a linked commit in that issue.

davidism avatar Feb 27 '17 13:02 davidism

Hmm, actually, the linked issue rolls back an earlier change. Would you provide a minimal example demonstrating what you need and what doesn't work right now?

davidism avatar Feb 27 '17 16:02 davidism

I want to specify an empty dictionary for the binds parameter. The example code looks like:

@pytest.yield_fixture()
def db_session(app, db):
    with app.app_context():
        connection = db.engine.connect()
        transaction = connection.begin()
        options = dict(bind=connection, binds={})
        session = db.create_scoped_session(options=options)
        db.session = session
        yield session
        transaction.rollback()
        connection.close()
        session.remove()

I would expect the transaction is rolled back, but it does not. By using the workaround in #345 , it works.

matt8754 avatar Feb 28 '17 00:02 matt8754

#1087 fixes a bunch of stuff about binds, including making the implementation of get_bind mirror how the base implementation works. I'm not sure if passing binds= as a session option would do anything though, given that Flask-SQAlchemy prefers looking up its configured binds, but it's now possible to customize the session class to do whatever you need by passing session_options={"class_": CustomSession}.

davidism avatar Sep 18 '22 17:09 davidism