SQLAlchemy session not working when I use it inside a with statement. It should work, since the sessionmaker
returns a Session
object that according to the source code does have __enter__
and __exit__
methods. I checked and I am in using the latest version (SQLAlchemy==1.3.22).
Code used to pinpoint the error:
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine(
'sqlite:////devdb/sqlite.db',
echo=True
)
Session = sessionmaker(bind=engine)
with Session() as session:
print('it worked!')
The Error:
Traceback (most recent call last):
File "test.py", line 11, in <module>
with Session() as session:
AttributeError: __enter__
I also tried my own class, just to test it, and same error occurred:
class MySessionMaker(sessionmaker):
def __init__(self, bind):
super().__init__(bind=bind)
def __enter__(self):
return self()
def __exit__(self, exception_type, exception_value, exception_traceback):
self.close()
Session = MySessionMaker(bind=engine)
I could not find anything online regarding this error, have anyone else encountered this problem?