Coverage for docs_src/tutorial/fastapi/update/tutorial001.py: 100%

59 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-16 00:02 +0000

1from typing import Annotated 1abcdefghijklm

2 

3from fastapi import FastAPI, HTTPException, Query 1abcdefghijklm

4from sqlmodel import Field, Session, SQLModel, create_engine, select 1abcdefghijklm

5 

6 

7class HeroBase(SQLModel): 1abcdefghijklm

8 name: str = Field(index=True) 1abcdefghijklm

9 secret_name: str 1abcdefghijklm

10 age: int | None = Field(default=None, index=True) 1abcdefghijklm

11 

12 

13class Hero(HeroBase, table=True): 1abcdefghijklm

14 id: int | None = Field(default=None, primary_key=True) 1abcdefghijklm

15 

16 

17class HeroCreate(HeroBase): 1abcdefghijklm

18 pass 1abcdefghijklm

19 

20 

21class HeroPublic(HeroBase): 1abcdefghijklm

22 id: int 1abcdefghijklm

23 

24 

25class HeroUpdate(SQLModel): 1abcdefghijklm

26 name: str | None = None 1abcdefghijklm

27 secret_name: str | None = None 1abcdefghijklm

28 age: int | None = None 1abcdefghijklm

29 

30 

31sqlite_file_name = "database.db" 1abcdefghijklm

32sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcdefghijklm

33 

34connect_args = {"check_same_thread": False} 1abcdefghijklm

35engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1abcdefghijklm

36 

37 

38def create_db_and_tables(): 1abcdefghijklm

39 SQLModel.metadata.create_all(engine) 1nopqrstuvwxyz

40 

41 

42app = FastAPI() 1abcdefghijklm

43 

44 

45@app.on_event("startup") 1abcdefghijklm

46def on_startup(): 1abcdefghijklm

47 create_db_and_tables() 1nopqrstuvwxyz

48 

49 

50@app.post("/heroes/", response_model=HeroPublic) 1abcdefghijklm

51def create_hero(hero: HeroCreate): 1abcdefghijklm

52 with Session(engine) as session: 1nopqrstuvwxyz

53 db_hero = Hero.model_validate(hero) 1nopqrstuvwxyz

54 session.add(db_hero) 1nopqrstuvwxyz

55 session.commit() 1nopqrstuvwxyz

56 session.refresh(db_hero) 1nopqrstuvwxyz

57 return db_hero 1nopqrstuvwxyz

58 

59 

60@app.get("/heroes/", response_model=list[HeroPublic]) 1abcdefghijklm

61def read_heroes(offset: int = 0, limit: Annotated[int, Query(le=100)] = 100): 1abcdefghijklm

62 with Session(engine) as session: 1nopqrstuvwxyz

63 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1nopqrstuvwxyz

64 return heroes 1nopqrstuvwxyz

65 

66 

67@app.get("/heroes/{hero_id}", response_model=HeroPublic) 1abcdefghijklm

68def read_hero(hero_id: int): 1abcdefghijklm

69 with Session(engine) as session: 1nopqrstuvwxyz

70 hero = session.get(Hero, hero_id) 1nopqrstuvwxyz

71 if not hero: 1nopqrstuvwxyz

72 raise HTTPException(status_code=404, detail="Hero not found") 1nopqrstuvwxyz

73 return hero 1nopqrstuvwxyz

74 

75 

76@app.patch("/heroes/{hero_id}", response_model=HeroPublic) 1abcdefghijklm

77def update_hero(hero_id: int, hero: HeroUpdate): 1abcdefghijklm

78 with Session(engine) as session: 1nopqrstuvwxyz

79 db_hero = session.get(Hero, hero_id) 1nopqrstuvwxyz

80 if not db_hero: 1nopqrstuvwxyz

81 raise HTTPException(status_code=404, detail="Hero not found") 1nopqrstuvwxyz

82 hero_data = hero.model_dump(exclude_unset=True) 1nopqrstuvwxyz

83 db_hero.sqlmodel_update(hero_data) 1nopqrstuvwxyz

84 session.add(db_hero) 1nopqrstuvwxyz

85 session.commit() 1nopqrstuvwxyz

86 session.refresh(db_hero) 1nopqrstuvwxyz

87 return db_hero 1nopqrstuvwxyz