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

55 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-27 00:03 +0000

1from typing import Optional 1klmnopqrst

2 

3from sqlmodel import Field, Session, SQLModel, create_engine 1klmnopqrst

4 

5 

6class Hero(SQLModel, table=True): 1klmnopqrst

7 id: Optional[int] = Field(default=None, primary_key=True) 1klmnopqrst

8 name: str 1klmnopqrst

9 secret_name: str 1klmnopqrst

10 age: Optional[int] = None 1klmnopqrst

11 

12 

13sqlite_file_name = "database.db" 1klmnopqrst

14sqlite_url = f"sqlite:///{sqlite_file_name}" 1klmnopqrst

15 

16engine = create_engine(sqlite_url, echo=True) 1klmnopqrst

17 

18 

19def create_db_and_tables(): 1klmnopqrst

20 SQLModel.metadata.create_all(engine) 1abcdefghij

21 

22 

23def create_heroes(): 1klmnopqrst

24 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") # (1)! 1abcdefghij

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

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

27 

28 print("Before interacting with the database") # (4)! 1abcdefghij

29 print("Hero 1:", hero_1) # (5)! 1abcdefghij

30 print("Hero 2:", hero_2) # (6)! 1abcdefghij

31 print("Hero 3:", hero_3) # (7)! 1abcdefghij

32 

33 with Session(engine) as session: # (8)! 1abcdefghij

34 session.add(hero_1) # (9)! 1abcdefghij

35 session.add(hero_2) # (10)! 1abcdefghij

36 session.add(hero_3) # (11)! 1abcdefghij

37 

38 print("After adding to the session") # (12)! 1abcdefghij

39 print("Hero 1:", hero_1) # (13)! 1abcdefghij

40 print("Hero 2:", hero_2) # (14)! 1abcdefghij

41 print("Hero 3:", hero_3) # (15)! 1abcdefghij

42 

43 session.commit() # (16)! 1abcdefghij

44 

45 print("After committing the session") # (17)! 1abcdefghij

46 print("Hero 1:", hero_1) # (18)! 1abcdefghij

47 print("Hero 2:", hero_2) # (19)! 1abcdefghij

48 print("Hero 3:", hero_3) # (20)! 1abcdefghij

49 

50 print("After committing the session, show IDs") # (21)! 1abcdefghij

51 print("Hero 1 ID:", hero_1.id) # (22)! 1abcdefghij

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

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

54 

55 print("After committing the session, show names") # (25)! 1abcdefghij

56 print("Hero 1 name:", hero_1.name) # (26)! 1abcdefghij

57 print("Hero 2 name:", hero_2.name) # (27)! 1abcdefghij

58 print("Hero 3 name:", hero_3.name) # (28)! 1abcdefghij

59 

60 session.refresh(hero_1) # (29)! 1abcdefghij

61 session.refresh(hero_2) # (30)! 1abcdefghij

62 session.refresh(hero_3) # (31)! 1abcdefghij

63 

64 print("After refreshing the heroes") # (32)! 1abcdefghij

65 print("Hero 1:", hero_1) # (33)! 1abcdefghij

66 print("Hero 2:", hero_2) # (34)! 1abcdefghij

67 print("Hero 3:", hero_3) # (35)! 1abcdefghij

68 # (36)! 

69 

70 print("After the session closes") # (37)! 1abcdefghij

71 print("Hero 1:", hero_1) # (38)! 1abcdefghij

72 print("Hero 2:", hero_2) # (39)! 1abcdefghij

73 print("Hero 3:", hero_3) # (40)! 1abcdefghij

74 

75 

76def main(): 1klmnopqrst

77 create_db_and_tables() 1abcdefghij

78 create_heroes() 1abcdefghij

79 

80 

81if __name__ == "__main__": 1klmnopqrst

82 main()