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

65 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 HeroTeamLink(SQLModel, table=True): 1abcdef

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

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

9 ) 

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

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

12 ) 

13 is_training: bool = False 1abcdef

14 

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

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

17 

18 

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

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

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

22 headquarters: str 1abcdef

23 

24 hero_links: List[HeroTeamLink] = Relationship(back_populates="team") 1abcdef

25 

26 

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

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

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

30 secret_name: str 1abcdef

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

32 

33 team_links: List[HeroTeamLink] = Relationship(back_populates="hero") 1abcdef

34 

35 

36sqlite_file_name = "database.db" 1abcdef

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

38 

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

40 

41 

42def create_db_and_tables(): 1abcdef

43 SQLModel.metadata.create_all(engine) 1abcdef

44 

45 

46def create_heroes(): 1abcdef

47 with Session(engine) as session: 1abcdef

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

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

50 

51 hero_deadpond = Hero( 1abcdef

52 name="Deadpond", 

53 secret_name="Dive Wilson", 

54 ) 

55 hero_rusty_man = Hero( 1abcdef

56 name="Rusty-Man", 

57 secret_name="Tommy Sharp", 

58 age=48, 

59 ) 

60 hero_spider_boy = Hero( 1abcdef

61 name="Spider-Boy", 

62 secret_name="Pedro Parqueador", 

63 ) 

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

65 deadpond_preventers_link = HeroTeamLink( 1abcdef

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

67 ) 

68 spider_boy_preventers_link = HeroTeamLink( 1abcdef

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

70 ) 

71 rusty_man_preventers_link = HeroTeamLink( 1abcdef

72 team=team_preventers, hero=hero_rusty_man 

73 ) 

74 

75 session.add(deadpond_team_z_link) 1abcdef

76 session.add(deadpond_preventers_link) 1abcdef

77 session.add(spider_boy_preventers_link) 1abcdef

78 session.add(rusty_man_preventers_link) 1abcdef

79 session.commit() 1abcdef

80 

81 for link in team_z_force.hero_links: 1abcdef

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

83 

84 for link in team_preventers.hero_links: 1abcdef

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

86 

87 

88def update_heroes(): 1abcdef

89 with Session(engine) as session: 1abcdef

90 hero_spider_boy = session.exec( 1abcdef

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

92 ).one() 

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

94 

95 spider_boy_z_force_link = HeroTeamLink( 1abcdef

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) 1abcdef

99 session.add(team_z_force) 1abcdef

100 session.commit() 1abcdef

101 

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

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

104 

105 for link in hero_spider_boy.team_links: 1abcdef

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

107 link.is_training = False 1abcdef

108 

109 session.add(hero_spider_boy) 1abcdef

110 session.commit() 1abcdef

111 

112 for link in hero_spider_boy.team_links: 1abcdef

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

114 

115 

116def main(): 1abcdef

117 create_db_and_tables() 1abcdef

118 create_heroes() 1abcdef

119 update_heroes() 1abcdef

120 

121 

122if __name__ == "__main__": 1abcdef

123 main()