Hi Brendan, how are you doing?
Regarding the article Guide to Using Databases with Python Postgres, SQLAlchemy, and Alembic, I applied this in my project:
from contextlib import contextmanager
@contextmanager
def session_scope():
session = Session()
try:
yield session
session.commit()
except Exception:
session.rollback()
raise
finally:
session.close()
but now, when I try to query something to pass to my service layer, I'm getting this error:
sqlalchemy.orm.exc.DetachedInstanceError: Instance <Driver at 0x7f5ce0464c40> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: http://sqlalche.me/e/14/bhk3)
this is my code:
def find_all(model, skip: int = 0, limit: int = 0):
with session_scope() as db:
r = db.query(model).offset(skip).limit(limit).all()
return r
Can you help me to understand whats is happing?