"autocommit=True is no longer supported"
session = scoped_session(sessionmaker(bind=engine, autocommit=True))
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 Can you please provide an example of how the session context works with sqlalchemy-mixins library? It doesn't work for me.
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 We can a config. So that it's enabled by default but can be switched off based on one's needs.