Coverage for docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py: 100%

89 statements  

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

1from typing import List, Optional 1abcdef

2 

3from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select 1abcdef

4 

5 

6class Team(SQLModel, table=True): 1abcdef

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

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

9 headquarters: str 1abcdef

10 

11 heroes: List["Hero"] = Relationship(back_populates="team") 1abcdef

12 

13 

14class Hero(SQLModel, table=True): 1abcdef

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

16 name: str = Field(index=True) 1abcdef

17 secret_name: str 1abcdef

18 age: Optional[int] = Field(default=None, index=True) 1abcdef

19 

20 team_id: Optional[int] = Field(default=None, foreign_key="team.id") 1abcdef

21 team: Optional[Team] = Relationship(back_populates="heroes") 1abcdef

22 

23 

24sqlite_file_name = "database.db" 1abcdef

25sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcdef

26 

27engine = create_engine(sqlite_url, echo=True) 1abcdef

28 

29 

30def create_db_and_tables(): 1abcdef

31 SQLModel.metadata.create_all(engine) 1abcdef

32 

33 

34def create_heroes(): 1abcdef

35 with Session(engine) as session: 1abcdef

36 team_preventers = Team(name="Preventers", headquarters="Sharp Tower") 1abcdef

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

38 

39 hero_deadpond = Hero( 1abcdef

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

41 ) 

42 hero_rusty_man = Hero( 1abcdef

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

44 ) 

45 hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") 1abcdef

46 session.add(hero_deadpond) 1abcdef

47 session.add(hero_rusty_man) 1abcdef

48 session.add(hero_spider_boy) 1abcdef

49 session.commit() 1abcdef

50 

51 session.refresh(hero_deadpond) 1abcdef

52 session.refresh(hero_rusty_man) 1abcdef

53 session.refresh(hero_spider_boy) 1abcdef

54 

55 print("Created hero:", hero_deadpond) 1abcdef

56 print("Created hero:", hero_rusty_man) 1abcdef

57 print("Created hero:", hero_spider_boy) 1abcdef

58 

59 hero_spider_boy.team = team_preventers 1abcdef

60 session.add(hero_spider_boy) 1abcdef

61 session.commit() 1abcdef

62 session.refresh(hero_spider_boy) 1abcdef

63 print("Updated hero:", hero_spider_boy) 1abcdef

64 

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

66 hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") 1abcdef

67 team_wakaland = Team( 1abcdef

68 name="Wakaland", 

69 headquarters="Wakaland Capital City", 

70 heroes=[hero_black_lion, hero_sure_e], 

71 ) 

72 session.add(team_wakaland) 1abcdef

73 session.commit() 1abcdef

74 session.refresh(team_wakaland) 1abcdef

75 print("Team Wakaland:", team_wakaland) 1abcdef

76 

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

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

79 hero_cap = Hero( 1abcdef

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

81 ) 

82 

83 team_preventers.heroes.append(hero_tarantula) 1abcdef

84 team_preventers.heroes.append(hero_dr_weird) 1abcdef

85 team_preventers.heroes.append(hero_cap) 1abcdef

86 session.add(team_preventers) 1abcdef

87 session.commit() 1abcdef

88 session.refresh(hero_tarantula) 1abcdef

89 session.refresh(hero_dr_weird) 1abcdef

90 session.refresh(hero_cap) 1abcdef

91 print("Preventers new hero:", hero_tarantula) 1abcdef

92 print("Preventers new hero:", hero_dr_weird) 1abcdef

93 print("Preventers new hero:", hero_cap) 1abcdef

94 

95 

96def select_heroes(): 1abcdef

97 with Session(engine) as session: 1abcdef

98 statement = select(Team).where(Team.name == "Preventers") 1abcdef

99 result = session.exec(statement) 1abcdef

100 team_preventers = result.one() 1abcdef

101 

102 print("Preventers heroes:", team_preventers.heroes) 1abcdef

103 

104 

105def update_heroes(): 1abcdef

106 with Session(engine) as session: 1abcdef

107 hero_spider_boy = session.exec( 1abcdef

108 select(Hero).where(Hero.name == "Spider-Boy") 

109 ).one() 

110 

111 preventers_team = session.exec( 1abcdef

112 select(Team).where(Team.name == "Preventers") 

113 ).one() 

114 

115 print("Hero Spider-Boy:", hero_spider_boy) 1abcdef

116 print("Preventers Team:", preventers_team) 1abcdef

117 print("Preventers Team Heroes:", preventers_team.heroes) 1abcdef

118 

119 hero_spider_boy.team = None 1abcdef

120 

121 print("Spider-Boy without team:", hero_spider_boy) 1abcdef

122 

123 print("Preventers Team Heroes again:", preventers_team.heroes) 1abcdef

124 

125 session.add(hero_spider_boy) 1abcdef

126 session.commit() 1abcdef

127 print("After committing") 1abcdef

128 

129 session.refresh(hero_spider_boy) 1abcdef

130 print("Spider-Boy after commit:", hero_spider_boy) 1abcdef

131 

132 print("Preventers Team Heroes after commit:", preventers_team.heroes) 1abcdef

133 

134 

135def main(): 1abcdef

136 create_db_and_tables() 1abcdef

137 create_heroes() 1abcdef

138 select_heroes() 1abcdef

139 update_heroes() 1abcdef

140 

141 

142if __name__ == "__main__": 1abcdef

143 main()