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

64 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-16 00:02 +0000

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

2 

3 

4class HeroTeamLink(SQLModel, table=True): 1nopqrstuvwxyz

5 team_id: int | None = Field(default=None, foreign_key="team.id", primary_key=True) 1nopqrstuvwxyz

6 hero_id: int | None = Field(default=None, foreign_key="hero.id", primary_key=True) 1nopqrstuvwxyz

7 is_training: bool = False 1nopqrstuvwxyz

8 

9 team: "Team" = Relationship(back_populates="hero_links") 1nopqrstuvwxyz

10 hero: "Hero" = Relationship(back_populates="team_links") 1nopqrstuvwxyz

11 

12 

13class Team(SQLModel, table=True): 1nopqrstuvwxyz

14 id: int | None = Field(default=None, primary_key=True) 1nopqrstuvwxyz

15 name: str = Field(index=True) 1nopqrstuvwxyz

16 headquarters: str 1nopqrstuvwxyz

17 

18 hero_links: list[HeroTeamLink] = Relationship(back_populates="team") 1nopqrstuvwxyz

19 

20 

21class Hero(SQLModel, table=True): 1nopqrstuvwxyz

22 id: int | None = Field(default=None, primary_key=True) 1nopqrstuvwxyz

23 name: str = Field(index=True) 1nopqrstuvwxyz

24 secret_name: str 1nopqrstuvwxyz

25 age: int | None = Field(default=None, index=True) 1nopqrstuvwxyz

26 

27 team_links: list[HeroTeamLink] = Relationship(back_populates="hero") 1nopqrstuvwxyz

28 

29 

30sqlite_file_name = "database.db" 1nopqrstuvwxyz

31sqlite_url = f"sqlite:///{sqlite_file_name}" 1nopqrstuvwxyz

32 

33engine = create_engine(sqlite_url, echo=True) 1nopqrstuvwxyz

34 

35 

36def create_db_and_tables(): 1nopqrstuvwxyz

37 SQLModel.metadata.create_all(engine) 1abcdefghijklm

38 

39 

40def create_heroes(): 1nopqrstuvwxyz

41 with Session(engine) as session: 1abcdefghijklm

42 team_preventers = Team(name="Preventers", headquarters="Sharp Tower") 1abcdefghijklm

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

44 

45 hero_deadpond = Hero( 1abcdefghijklm

46 name="Deadpond", 

47 secret_name="Dive Wilson", 

48 ) 

49 hero_rusty_man = Hero( 1abcdefghijklm

50 name="Rusty-Man", 

51 secret_name="Tommy Sharp", 

52 age=48, 

53 ) 

54 hero_spider_boy = Hero( 1abcdefghijklm

55 name="Spider-Boy", 

56 secret_name="Pedro Parqueador", 

57 ) 

58 deadpond_team_z_link = HeroTeamLink(team=team_z_force, hero=hero_deadpond) 1abcdefghijklm

59 deadpond_preventers_link = HeroTeamLink( 1abcdefghijklm

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

61 ) 

62 spider_boy_preventers_link = HeroTeamLink( 1abcdefghijklm

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

64 ) 

65 rusty_man_preventers_link = HeroTeamLink( 1abcdefghijklm

66 team=team_preventers, hero=hero_rusty_man 

67 ) 

68 

69 session.add(deadpond_team_z_link) 1abcdefghijklm

70 session.add(deadpond_preventers_link) 1abcdefghijklm

71 session.add(spider_boy_preventers_link) 1abcdefghijklm

72 session.add(rusty_man_preventers_link) 1abcdefghijklm

73 session.commit() 1abcdefghijklm

74 

75 for link in team_z_force.hero_links: 1abcdefghijklm

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

77 

78 for link in team_preventers.hero_links: 1abcdefghijklm

79 print("Preventers hero:", link.hero, "is training:", link.is_training) 1abcdefghijklm

80 

81 

82def update_heroes(): 1nopqrstuvwxyz

83 with Session(engine) as session: 1abcdefghijklm

84 hero_spider_boy = session.exec( 1abcdefghijklm

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

86 ).one() 

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

88 

89 spider_boy_z_force_link = HeroTeamLink( 1abcdefghijklm

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

91 ) 

92 team_z_force.hero_links.append(spider_boy_z_force_link) 1abcdefghijklm

93 session.add(team_z_force) 1abcdefghijklm

94 session.commit() 1abcdefghijklm

95 

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

97 print("Z-Force heroes:", team_z_force.hero_links) 1abcdefghijklm

98 

99 for link in hero_spider_boy.team_links: 1abcdefghijklm

100 if link.team.name == "Preventers": 1abcdefghijklm

101 link.is_training = False 1abcdefghijklm

102 

103 session.add(hero_spider_boy) 1abcdefghijklm

104 session.commit() 1abcdefghijklm

105 

106 for link in hero_spider_boy.team_links: 1abcdefghijklm

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

108 

109 

110def main(): 1nopqrstuvwxyz

111 create_db_and_tables() 1abcdefghijklm

112 create_heroes() 1abcdefghijklm

113 update_heroes() 1abcdefghijklm

114 

115 

116if __name__ == "__main__": 1nopqrstuvwxyz

117 main()