Browse Source

Replace deprecated @app.on_event("startup") with lifespan context manager

Refactor FastAPI app initialization to use async lifespan.
pull/15697/head
Achyuta Dixit 1 month ago
committed by GitHub
parent
commit
5c5aeb4cf0
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 13
      docs_src/sql_databases/tutorial002_an_py310.py

13
docs_src/sql_databases/tutorial002_an_py310.py

@ -1,5 +1,5 @@
from contextlib import asynccontextmanager
from typing import Annotated
from fastapi import Depends, FastAPI, HTTPException, Query
from sqlmodel import Field, Session, SQLModel, create_engine, select
@ -45,12 +45,13 @@ def get_session():
SessionDep = Annotated[Session, Depends(get_session)]
app = FastAPI()
@app.on_event("startup")
def on_startup():
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield
app = FastAPI(lifespan=lifespan)
@app.post("/heroes/", response_model=HeroPublic)

Loading…
Cancel
Save