Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Automatically run session.commit() in session dependency BEFORE returning request #3620

Description

@anguspmitchell

First 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions