Skip to content

Commit 3ef44a5

Browse files
committed
fix create db and warnings
1 parent 5d0c23c commit 3ef44a5

16 files changed

Lines changed: 95 additions & 62 deletions

File tree

docs_src/tutorial/fastapi/app_testing/tutorial001_py310/main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import asynccontextmanager
12
from fastapi import Depends, FastAPI, HTTPException, Query
23
from sqlmodel import Field, Session, SQLModel, create_engine, select
34

@@ -42,12 +43,13 @@ def get_session():
4243
yield session
4344

4445

45-
app = FastAPI()
46+
@asynccontextmanager
47+
async def lifespan(app: FastAPI):
48+
create_db_and_tables()
49+
yield
4650

4751

48-
@app.on_event("startup")
49-
def on_startup():
50-
create_db_and_tables()
52+
app = FastAPI(lifespan=lifespan)
5153

5254

5355
@app.post("/heroes/", response_model=HeroPublic)

docs_src/tutorial/fastapi/delete/tutorial001_py310.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import asynccontextmanager
2+
13
from fastapi import FastAPI, HTTPException, Query
24
from sqlmodel import Field, Session, SQLModel, create_engine, select
35

@@ -37,12 +39,13 @@ def create_db_and_tables():
3739
SQLModel.metadata.create_all(engine)
3840

3941

40-
app = FastAPI()
42+
@asynccontextmanager
43+
async def lifespan(app: FastAPI):
44+
create_db_and_tables()
45+
yield
4146

4247

43-
@app.on_event("startup")
44-
def on_startup():
45-
create_db_and_tables()
48+
app = FastAPI(lifespan=lifespan)
4649

4750

4851
@app.post("/heroes/", response_model=HeroPublic)

docs_src/tutorial/fastapi/limit_and_offset/tutorial001_py310.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import asynccontextmanager
2+
13
from fastapi import FastAPI, HTTPException, Query
24
from sqlmodel import Field, Session, SQLModel, create_engine, select
35

@@ -31,12 +33,13 @@ def create_db_and_tables():
3133
SQLModel.metadata.create_all(engine)
3234

3335

34-
app = FastAPI()
36+
@asynccontextmanager
37+
async def lifespan(app: FastAPI):
38+
create_db_and_tables()
39+
yield
3540

3641

37-
@app.on_event("startup")
38-
def on_startup():
39-
create_db_and_tables()
42+
app = FastAPI(lifespan=lifespan)
4043

4144

4245
@app.post("/heroes/", response_model=HeroPublic)

docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import asynccontextmanager
2+
13
from fastapi import FastAPI
24
from sqlmodel import Field, Session, SQLModel, create_engine, select
35

@@ -33,12 +35,13 @@ def create_db_and_tables():
3335
SQLModel.metadata.create_all(engine)
3436

3537

36-
app = FastAPI()
38+
@asynccontextmanager
39+
async def lifespan(app: FastAPI):
40+
create_db_and_tables()
41+
yield
3742

3843

39-
@app.on_event("startup")
40-
def on_startup():
41-
create_db_and_tables()
44+
app = FastAPI(lifespan=lifespan)
4245

4346

4447
@app.post("/heroes/", response_model=HeroPublic)

docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import asynccontextmanager
2+
13
from fastapi import FastAPI
24
from sqlmodel import Field, Session, SQLModel, create_engine, select
35

@@ -31,12 +33,13 @@ def create_db_and_tables():
3133
SQLModel.metadata.create_all(engine)
3234

3335

34-
app = FastAPI()
36+
@asynccontextmanager
37+
async def lifespan(app: FastAPI):
38+
create_db_and_tables()
39+
yield
3540

3641

37-
@app.on_event("startup")
38-
def on_startup():
39-
create_db_and_tables()
42+
app = FastAPI(lifespan=lifespan)
4043

4144

4245
@app.post("/heroes/", response_model=HeroPublic)

docs_src/tutorial/fastapi/read_one/tutorial001_py310.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import asynccontextmanager
2+
13
from fastapi import FastAPI, HTTPException
24
from sqlmodel import Field, Session, SQLModel, create_engine, select
35

@@ -31,12 +33,13 @@ def create_db_and_tables():
3133
SQLModel.metadata.create_all(engine)
3234

3335

34-
app = FastAPI()
36+
@asynccontextmanager
37+
async def lifespan(app: FastAPI):
38+
create_db_and_tables()
39+
yield
3540

3641

37-
@app.on_event("startup")
38-
def on_startup():
39-
create_db_and_tables()
42+
app = FastAPI(lifespan=lifespan)
4043

4144

4245
@app.post("/heroes/", response_model=HeroPublic)

docs_src/tutorial/fastapi/relationships/tutorial001_py310.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import asynccontextmanager
2+
13
from fastapi import Depends, FastAPI, HTTPException, Query
24
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select
35

@@ -80,12 +82,13 @@ def get_session():
8082
yield session
8183

8284

83-
app = FastAPI()
85+
@asynccontextmanager
86+
async def lifespan(app: FastAPI):
87+
create_db_and_tables()
88+
yield
8489

8590

86-
@app.on_event("startup")
87-
def on_startup():
88-
create_db_and_tables()
91+
app = FastAPI(lifespan=lifespan)
8992

9093

9194
@app.post("/heroes/", response_model=HeroPublic)

docs_src/tutorial/fastapi/response_model/tutorial001_py310.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import asynccontextmanager
2+
13
from fastapi import FastAPI
24
from sqlmodel import Field, Session, SQLModel, create_engine, select
35

@@ -20,12 +22,13 @@ def create_db_and_tables():
2022
SQLModel.metadata.create_all(engine)
2123

2224

23-
app = FastAPI()
25+
@asynccontextmanager
26+
async def lifespan(app: FastAPI):
27+
create_db_and_tables()
28+
yield
2429

2530

26-
@app.on_event("startup")
27-
def on_startup():
28-
create_db_and_tables()
31+
app = FastAPI(lifespan=lifespan)
2932

3033

3134
@app.post("/heroes/", response_model=Hero)

docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import asynccontextmanager
2+
13
from fastapi import Depends, FastAPI, HTTPException, Query
24
from sqlmodel import Field, Session, SQLModel, create_engine, select
35

@@ -42,12 +44,13 @@ def get_session():
4244
yield session
4345

4446

45-
app = FastAPI()
47+
@asynccontextmanager
48+
async def lifespan(app: FastAPI):
49+
create_db_and_tables()
50+
yield
4651

4752

48-
@app.on_event("startup")
49-
def on_startup():
50-
create_db_and_tables()
53+
app = FastAPI(lifespan=lifespan)
5154

5255

5356
@app.post("/heroes/", response_model=HeroPublic)

docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import asynccontextmanager
2+
13
from fastapi import FastAPI
24
from sqlmodel import Field, Session, SQLModel, create_engine, select
35

@@ -20,12 +22,13 @@ def create_db_and_tables():
2022
SQLModel.metadata.create_all(engine)
2123

2224

23-
app = FastAPI()
25+
@asynccontextmanager
26+
async def lifespan(app: FastAPI):
27+
create_db_and_tables()
28+
yield
2429

2530

26-
@app.on_event("startup")
27-
def on_startup():
28-
create_db_and_tables()
31+
app = FastAPI(lifespan=lifespan)
2932

3033

3134
@app.post("/heroes/")

0 commit comments

Comments
 (0)