Coverage for docs_src/tutorial/many_to_many/tutorial003_py39.py: 100%

65 statements  

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

1from typing import Optional 1abcd

2 

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

4 

5 

6class HeroTeamLink(SQLModel, table=True): 1abcd

7 team_id: Optional[int] = Field( 1abcd

8 default=None, foreign_key="team.id", primary_key=True 

9 ) 

10 hero_id: Optional[int] = Field( 1abcd

11 default=None, foreign_key="hero.id", primary_key=True 

12 ) 

13 is_training: bool = False 1abcd

14 

15 team: "Team" = Relationship(back_populates="hero_links") 1abcd

16 hero: "Hero" = Relationship(back_populates="team_links") 1abcd

17 

18 

19class Team(SQLModel, table=True): 1abcd

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

21 name: str = Field(index=True) 1abcd

22 headquarters: str 1abcd

23 

24 hero_links: list[HeroTeamLink] = Relationship(back_populates="team") 1abcd

25 

26 

27class Hero(SQLModel, table=True): 1abcd

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

29 name: str = Field(index=True) 1abcd

30 secret_name: str 1abcd

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

32 

33 team_links: list[HeroTeamLink] = Relationship(back_populates="hero") 1abcd

34 

35 

36sqlite_file_name = "database.db" 1abcd

37sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcd

38 

39engine = create_engine(sqlite_url, echo=True) 1abcd

40 

41 

42def create_db_and_tables(): 1abcd

43 SQLModel.metadata.create_all(engine) 1abcd

44 

45 

46def create_heroes(): 1abcd

47 with Session(engine) as session: 1abcd

48 team_preventers = Team(name="Preventers", headquarters="Sharp Tower") 1abcd

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

50 

51 hero_deadpond = Hero( 1abcd

52 name="Deadpond", 

53 secret_name="Dive Wilson", 

54 ) 

55 hero_rusty_man = Hero( 1abcd

56 name="Rusty-Man", 

57 secret_name="Tommy Sharp", 

58 age=48, 

59 ) 

60 hero_spider_boy = Hero( 1abcd

61 name="Spider-Boy", 

62 secret_name="Pedro Parqueador", 

63 ) 

64 deadpond_team_z_link = HeroTeamLink(team=team_z_force, hero=hero_deadpond) 1abcd

65 deadpond_preventers_link = HeroTeamLink( 1abcd

66 team=team_preventers, hero=hero_deadpond, is_training=True 

67 ) 

68 spider_boy_preventers_link = HeroTeamLink( 1abcd

69 team=team_preventers, hero=hero_spider_boy, is_training=True 

70 ) 

71 rusty_man_preventers_link = HeroTeamLink( 1abcd

72 team=team_preventers, hero=hero_rusty_man 

73 ) 

74 

75 session.add(deadpond_team_z_link) 1abcd

76 session.add(deadpond_preventers_link) 1abcd

77 session.add(spider_boy_preventers_link) 1abcd

78 session.add(rusty_man_preventers_link) 1abcd

79 session.commit() 1abcd

80 

81 for link in team_z_force.hero_links: 1abcd

82 print("Z-Force hero:", link.hero, "is training:", link.is_training) 1abcd

83 

84 for link in team_preventers.hero_links: 1abcd

85 print("Preventers hero:", link.hero, "is training:", link.is_training) 1abcd

86 

87 

88def update_heroes(): 1abcd

89 with Session(engine) as session: 1abcd

90 hero_spider_boy = session.exec( 1abcd

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

92 ).one() 

93 team_z_force = session.exec(select(Team).where(Team.name == "Z-Force")).one() 1abcd

94 

95 spider_boy_z_force_link = HeroTeamLink( 1abcd

96 team=team_z_force, hero=hero_spider_boy, is_training=True 

97 ) 

98 team_z_force.hero_links.append(spider_boy_z_force_link) 1abcd

99 session.add(team_z_force) 1abcd

100 session.commit() 1abcd

101 

102 print("Updated Spider-Boy's Teams:", hero_spider_boy.team_links) 1abcd

103 print("Z-Force heroes:", team_z_force.hero_links) 1abcd

104 

105 for link in hero_spider_boy.team_links: 1abcd

106 if link.team.name == "Preventers": 1abcd

107 link.is_training = False 1abcd

108 

109 session.add(hero_spider_boy) 1abcd

110 session.commit() 1abcd

111 

112 for link in hero_spider_boy.team_links: 1abcd

113 print("Spider-Boy team:", link.team, "is training:", link.is_training) 1abcd

114 

115 

116def main(): 1abcd

117 create_db_and_tables() 1abcd

118 create_heroes() 1abcd

119 update_heroes() 1abcd

120 

121 

122if __name__ == "__main__": 1abcd

123 main()