question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
AlexanderPodorovcommented, Jul 29, 2021

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:

Session = sesssionmaker(engine)

with Session() as session:
    with session.begin():
        session.add(some_object)
    # commits

# closes the Session
2reactions
AlexanderPodorovcommented, Jul 28, 2021

@anguspmitchell , I think you should avoid dependencies like this:

@contextmanager
def session_scope(expire_on_commit: bool = True) -> sqlalchemy.orm.session.Session:
    orm_session = _orm_session(expire_on_commit=expire_on_commit)
    try:
        yield orm_session
        orm_session.commit()
    except:
        orm_session.rollback()
        raise
    finally:
        orm_session.close()

For those reasons:

  1. as @waynerv said, the statement after yield will be executed after the response is returned. So that means, in some circumstances you will send OK response, but commit will actually fail.
  2. If you raise HTTPException from endpoint function body, then session will be commited too. This kind of exception won’t be caught by try-except of session_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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Session API — SQLAlchemy 2.0 Documentation
Automatically start transactions (i.e. equivalent to invoking Session.begin() ) when database access is requested by an operation.
Read more >
Session with FastAPI Dependency - SQLModel
A FastAPI dependency is very simple, it's just a function that returns a value. It could use yield instead of return , and...
Read more >
FastApi Sqlalchemy how to manage transaction (session and ...
I had the same problem while using FastAPI. I couldn't find a way to use commit in separate methods and have them behave...
Read more >
Session - API Manual
begin() method is used to explicitly start transactions. See also. Autocommit Mode. autoflush¶ – When True , all query operations will issue a ......
Read more >
Advanced Usage — Requests 2.28.1 documentation
This is done by providing data to the properties on a Session object: s = requests.Session() s.auth = ('user', 'pass') s.headers.update({'x-test': 'true'}) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found