Coverage for docs_src / tutorial / automatic_id_none_refresh / tutorial002_py310.py: 100%

54 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-01-06 21:09 +0000

1from sqlmodel import Field, Session, SQLModel, create_engine 1ijklmnop

2 

3 

4class Hero(SQLModel, table=True): 1ijklmnop

5 id: int | None = Field(default=None, primary_key=True) 1ijklmnop

6 name: str 1ijklmnop

7 secret_name: str 1ijklmnop

8 age: int | None = None 1ijklmnop

9 

10 

11sqlite_file_name = "database.db" 1ijklmnop

12sqlite_url = f"sqlite:///{sqlite_file_name}" 1ijklmnop

13 

14engine = create_engine(sqlite_url, echo=True) 1ijklmnop

15 

16 

17def create_db_and_tables(): 1ijklmnop

18 SQLModel.metadata.create_all(engine) 1abcdefgh

19 

20 

21def create_heroes(): 1ijklmnop

22 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") # (1)! 1abcdefgh

23 hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") # (2)! 1abcdefgh

24 hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) # (3)! 1abcdefgh

25 

26 print("Before interacting with the database") # (4)! 1abcdefgh

27 print("Hero 1:", hero_1) # (5)! 1abcdefgh

28 print("Hero 2:", hero_2) # (6)! 1abcdefgh

29 print("Hero 3:", hero_3) # (7)! 1abcdefgh

30 

31 with Session(engine) as session: # (8)! 1abcdefgh

32 session.add(hero_1) # (9)! 1abcdefgh

33 session.add(hero_2) # (10)! 1abcdefgh

34 session.add(hero_3) # (11)! 1abcdefgh

35 

36 print("After adding to the session") # (12)! 1abcdefgh

37 print("Hero 1:", hero_1) # (13)! 1abcdefgh

38 print("Hero 2:", hero_2) # (14)! 1abcdefgh

39 print("Hero 3:", hero_3) # (15)! 1abcdefgh

40 

41 session.commit() # (16)! 1abcdefgh

42 

43 print("After committing the session") # (17)! 1abcdefgh

44 print("Hero 1:", hero_1) # (18)! 1abcdefgh

45 print("Hero 2:", hero_2) # (19)! 1abcdefgh

46 print("Hero 3:", hero_3) # (20)! 1abcdefgh

47 

48 print("After committing the session, show IDs") # (21)! 1abcdefgh

49 print("Hero 1 ID:", hero_1.id) # (22)! 1abcdefgh

50 print("Hero 2 ID:", hero_2.id) # (23)! 1abcdefgh

51 print("Hero 3 ID:", hero_3.id) # (24)! 1abcdefgh

52 

53 print("After committing the session, show names") # (25)! 1abcdefgh

54 print("Hero 1 name:", hero_1.name) # (26)! 1abcdefgh

55 print("Hero 2 name:", hero_2.name) # (27)! 1abcdefgh

56 print("Hero 3 name:", hero_3.name) # (28)! 1abcdefgh

57 

58 session.refresh(hero_1) # (29)! 1abcdefgh

59 session.refresh(hero_2) # (30)! 1abcdefgh

60 session.refresh(hero_3) # (31)! 1abcdefgh

61 

62 print("After refreshing the heroes") # (32)! 1abcdefgh

63 print("Hero 1:", hero_1) # (33)! 1abcdefgh

64 print("Hero 2:", hero_2) # (34)! 1abcdefgh

65 print("Hero 3:", hero_3) # (35)! 1abcdefgh

66 # (36)! 

67 

68 print("After the session closes") # (37)! 1abcdefgh

69 print("Hero 1:", hero_1) # (38)! 1abcdefgh

70 print("Hero 2:", hero_2) # (39)! 1abcdefgh

71 print("Hero 3:", hero_3) # (40)! 1abcdefgh

72 

73 

74def main(): 1ijklmnop

75 create_db_and_tables() 1abcdefgh

76 create_heroes() 1abcdefgh

77 

78 

79if __name__ == "__main__": 1ijklmnop

80 main()