Automatically run `session.commit()` in session dependency BEFORE returning request
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
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
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top GitHub Comments
Yes, I think this approach should work. And yes, you can create a session in the endpoint function. My 1. and 2. points are related to dependency approach. BTW, as of SQLAlchemy 1.4 we don’t need to write our own context managers for sessions/transactions, it should work out of box. See: https://docs.sqlalchemy.org/en/14/orm/session_transaction.html Example for their site:
@anguspmitchell , I think you should avoid dependencies like this:
For those reasons:
HTTPException
from endpoint function body, then session will be commited too. This kind of exception won’t be caught bytry-except
ofsession_scope
function.P.S. I also tried to use the same approach in the very beginning of my work with FastAPI, but finally I’m confident it’s much better to commit explicitly at the end of endpoint function body.