Browse Source

Update tutorial002.py

Replace deprecated `@app.on_event("startup")` with lifecycle event approach.
pull/14214/head
Mike iLL Kilmer 9 months ago
committed by GitHub
parent
commit
dd2137cbe5
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 14
      docs_src/sql_databases/tutorial002.py

14
docs_src/sql_databases/tutorial002.py

@ -1,4 +1,5 @@
from typing import List, Union
from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI, HTTPException, Query
from sqlmodel import Field, Session, SQLModel, create_engine, select
@ -43,14 +44,13 @@ def get_session():
with Session(engine) as session:
yield session
@asynccontextmanager
async def lifespan(app: FastAPI):
models.create_db_and_tables()
yield
# Can add events for after startup completion here
app = FastAPI()
@app.on_event("startup")
def on_startup():
create_db_and_tables()
app = FastAPI(lifespan=lifespan)
@app.post("/heroes/", response_model=HeroPublic)
def create_hero(hero: HeroCreate, session: Session = Depends(get_session)):

Loading…
Cancel
Save