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

"autocommit=True is no longer supported"

Open 7dir opened this issue 1 year ago • 4 comments

session = scoped_session(sessionmaker(bind=engine, autocommit=True))

7dir avatar Aug 25 '24 21:08 7dir

Yet to fully test but the below code seems to work at this point of time : Reference: Sqlachemy 2.0 Migration Guide

@contextmanager
def session_scope():
    """Provide a transactional scope around a series of operations."""
    session = SessionLocal()
    try:
        yield session
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()


with session_scope() as session:
    BaseModel.set_session(session)

challapradyumna avatar Oct 20 '24 07:10 challapradyumna

@challapradyumna Can you please provide an example of how the session context works with sqlalchemy-mixins library? It doesn't work for me.

gnanesh-fiddler avatar Jan 09 '25 13:01 gnanesh-fiddler

The doc says that autocommit is required for sqlalchemy-mixins to work, but on the other hand the ActiveRecord methods all call this method by default, which commits/rollbacks the outermost transaction:

    def _commit_or_fail(self):
        try:
            self.session.commit()
        except:
            self.session.rollback()
            raise

This is harmful e.g in the "ATOMIC REQUESTS" mode of some servers ; to me, the Active Record mixin should not deal with transactions at all, and let end users deal with that.

pakal avatar Feb 21 '25 09:02 pakal

@pakal We can a config. So that it's enabled by default but can be switched off based on one's needs.

michaelbukachi avatar Feb 22 '25 21:02 michaelbukachi