First Check
Commit to Help
Example Code
# orm.py
def fastapi_session_scope() -> sqlalchemy.orm.session.Session:
session = session_maker(bind=engine)
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
# routers.py
@router.post("/v1/organizations", response_model=Organization)
def create_org(session: Session = Depends(orm.fastapi_session_scope)):
org = sqlalc.Organization(name="test")
session.add(org)
return org
@router.get("/v1/organizations", response_model=List[Organization])
def get_orgs(session: Session = Depends(orm.fastapi_session_scope)):
return session.query(sqlalc.Organization).all()
Description
(I realize this is diverging slightly from the SQLAlchemy example, which explicitly uses session.commit() in the body of the function and not in the session dependency.)
I'm creating a row with a POST and then immediately afterwards fetching all rows with a GET. I've got session.commit() in the session scope dependency, which is convenient because it runs a commit() at the end no matter what and doesn't require us to always explicitly commit at the end of an endpoint body.
As written, if an Organization is created via a POST and then all the organizations are immediately afterwards fetched via a GET, the GET will not include the recently created Organization. This is not a JS async issue. What's happening, as best I can tell, is that the POST starts a transaction, org is returned, and then, before the commit() actually happens, the GET is called and the list of organizations is returned without the organization that was just created (or so it seemed).
t POST GET
1 request
2 session.add
3 return
4 request
5 session.query
6 return # POSTed organization is missing!
7 session.commit
8 session.commit
The behavior I'd like is for commit() to run BEFORE the value is returned, which would guarantee that the POST and GET could be run back-to-back safely. Is that possible using a FastAPI dependency? Has anyone else run into this issue?
t POST GET
1 request
2 session.add
3 session.commit
4 return
5 request
6 session.query
7 session.commit
8 return
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.59.0
Python Version
3.8.10
Additional Context
No response
First Check
Commit to Help
Example Code
Description
(I realize this is diverging slightly from the SQLAlchemy example, which explicitly uses
session.commit()in the body of the function and not in the session dependency.)I'm creating a row with a POST and then immediately afterwards fetching all rows with a GET. I've got
session.commit()in the session scope dependency, which is convenient because it runs acommit()at the end no matter what and doesn't require us to always explicitly commit at the end of an endpoint body.As written, if an Organization is created via a POST and then all the organizations are immediately afterwards fetched via a GET, the GET will not include the recently created Organization. This is not a JS async issue. What's happening, as best I can tell, is that the POST starts a transaction,
orgis returned, and then, before thecommit()actually happens, the GET is called and the list of organizations is returned without the organization that was just created (or so it seemed).The behavior I'd like is for
commit()to run BEFORE the value is returned, which would guarantee that the POST and GET could be run back-to-back safely. Is that possible using a FastAPI dependency? Has anyone else run into this issue?Operating System
macOS
Operating System Details
No response
FastAPI Version
0.59.0
Python Version
3.8.10
Additional Context
No response