Coverage for docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py: 100%

82 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select 1abc

2 

3 

4class Team(SQLModel, table=True): 1abc

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

6 name: str = Field(index=True) 1abc

7 headquarters: str 1abc

8 

9 heroes: list["Hero"] = Relationship(back_populates="team") 1abc

10 

11 

12class Hero(SQLModel, table=True): 1abc

13 id: int | None = Field(default=None, primary_key=True) 1abc

14 name: str = Field(index=True) 1abc

15 secret_name: str 1abc

16 age: int | None = Field(default=None, index=True) 1abc

17 

18 team_id: int | None = Field(default=None, foreign_key="team.id") 1abc

19 team: Team | None = Relationship(back_populates="heroes") 1abc

20 

21 

22sqlite_file_name = "database.db" 1abc

23sqlite_url = f"sqlite:///{sqlite_file_name}" 1abc

24 

25engine = create_engine(sqlite_url, echo=True) 1abc

26 

27 

28def create_db_and_tables(): 1abc

29 SQLModel.metadata.create_all(engine) 1abc

30 

31 

32def create_heroes(): 1abc

33 with Session(engine) as session: 1abc

34 team_preventers = Team(name="Preventers", headquarters="Sharp Tower") 1abc

35 team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") 1abc

36 

37 hero_deadpond = Hero( 1abc

38 name="Deadpond", secret_name="Dive Wilson", team=team_z_force 

39 ) 

40 hero_rusty_man = Hero( 1abc

41 name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers 

42 ) 

43 hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") 1abc

44 session.add(hero_deadpond) 1abc

45 session.add(hero_rusty_man) 1abc

46 session.add(hero_spider_boy) 1abc

47 session.commit() 1abc

48 

49 session.refresh(hero_deadpond) 1abc

50 session.refresh(hero_rusty_man) 1abc

51 session.refresh(hero_spider_boy) 1abc

52 

53 print("Created hero:", hero_deadpond) 1abc

54 print("Created hero:", hero_rusty_man) 1abc

55 print("Created hero:", hero_spider_boy) 1abc

56 

57 hero_spider_boy.team = team_preventers 1abc

58 session.add(hero_spider_boy) 1abc

59 session.commit() 1abc

60 session.refresh(hero_spider_boy) 1abc

61 print("Updated hero:", hero_spider_boy) 1abc

62 

63 hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) 1abc

64 hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") 1abc

65 team_wakaland = Team( 1abc

66 name="Wakaland", 

67 headquarters="Wakaland Capital City", 

68 heroes=[hero_black_lion, hero_sure_e], 

69 ) 

70 session.add(team_wakaland) 1abc

71 session.commit() 1abc

72 session.refresh(team_wakaland) 1abc

73 print("Team Wakaland:", team_wakaland) 1abc

74 

75 hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) 1abc

76 hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) 1abc

77 hero_cap = Hero( 1abc

78 name="Captain North America", secret_name="Esteban Rogelios", age=93 

79 ) 

80 

81 team_preventers.heroes.append(hero_tarantula) 1abc

82 team_preventers.heroes.append(hero_dr_weird) 1abc

83 team_preventers.heroes.append(hero_cap) 1abc

84 session.add(team_preventers) 1abc

85 session.commit() 1abc

86 session.refresh(hero_tarantula) 1abc

87 session.refresh(hero_dr_weird) 1abc

88 session.refresh(hero_cap) 1abc

89 print("Preventers new hero:", hero_tarantula) 1abc

90 print("Preventers new hero:", hero_dr_weird) 1abc

91 print("Preventers new hero:", hero_cap) 1abc

92 

93 

94def select_heroes(): 1abc

95 with Session(engine) as session: 1abc

96 statement = select(Team).where(Team.name == "Preventers") 1abc

97 result = session.exec(statement) 1abc

98 team_preventers = result.one() 1abc

99 

100 print("Preventers heroes:", team_preventers.heroes) 1abc

101 

102 

103def update_heroes(): 1abc

104 with Session(engine) as session: 1abc

105 statement = select(Hero).where(Hero.name == "Spider-Boy") 1abc

106 result = session.exec(statement) 1abc

107 hero_spider_boy = result.one() 1abc

108 

109 hero_spider_boy.team = None 1abc

110 session.add(hero_spider_boy) 1abc

111 session.commit() 1abc

112 

113 session.refresh(hero_spider_boy) 1abc

114 print("Spider-Boy without team:", hero_spider_boy) 1abc

115 

116 

117def main(): 1abc

118 create_db_and_tables() 1abc

119 create_heroes() 1abc

120 select_heroes() 1abc

121 update_heroes() 1abc

122 

123 

124if __name__ == "__main__": 1abc

125 main()