Browse Source
Replace deprecated @app.on_event("startup") with lifespan context manager
pull/15697/head
Achyuta Dixit
1 month ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with
6 additions and
6 deletions
-
docs_src/sql_databases/tutorial001_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 |
|
|
|
|
|
|
|
@ -29,12 +29,12 @@ 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/") |
|
|
|
|