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

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

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

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

7 is_training: bool = False 1abc

8 

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

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

11 

12 

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

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

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

16 headquarters: str 1abc

17 

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

19 

20 

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

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

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

24 secret_name: str 1abc

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

26 

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

28 

29 

30sqlite_file_name = "database.db" 1abc

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

32 

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

34 

35 

36def create_db_and_tables(): 1abc

37 SQLModel.metadata.create_all(engine) 1abc

38 

39 

40def create_heroes(): 1abc

41 with Session(engine) as session: 1abc

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

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

44 

45 hero_deadpond = Hero( 1abc

46 name="Deadpond", 

47 secret_name="Dive Wilson", 

48 ) 

49 hero_rusty_man = Hero( 1abc

50 name="Rusty-Man", 

51 secret_name="Tommy Sharp", 

52 age=48, 

53 ) 

54 hero_spider_boy = Hero( 1abc

55 name="Spider-Boy", 

56 secret_name="Pedro Parqueador", 

57 ) 

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

59 deadpond_preventers_link = HeroTeamLink( 1abc

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

61 ) 

62 spider_boy_preventers_link = HeroTeamLink( 1abc

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

64 ) 

65 rusty_man_preventers_link = HeroTeamLink( 1abc

66 team=team_preventers, hero=hero_rusty_man 

67 ) 

68 

69 session.add(deadpond_team_z_link) 1abc

70 session.add(deadpond_preventers_link) 1abc

71 session.add(spider_boy_preventers_link) 1abc

72 session.add(rusty_man_preventers_link) 1abc

73 session.commit() 1abc

74 

75 for link in team_z_force.hero_links: 1abc

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

77 

78 for link in team_preventers.hero_links: 1abc

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

80 

81 

82def update_heroes(): 1abc

83 with Session(engine) as session: 1abc

84 hero_spider_boy = session.exec( 1abc

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

86 ).one() 

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

88 

89 spider_boy_z_force_link = HeroTeamLink( 1abc

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

93 session.add(team_z_force) 1abc

94 session.commit() 1abc

95 

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

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

98 

99 for link in hero_spider_boy.team_links: 1abc

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

101 link.is_training = False 1abc

102 

103 session.add(hero_spider_boy) 1abc

104 session.commit() 1abc

105 

106 for link in hero_spider_boy.team_links: 1abc

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

108 

109 

110def main(): 1abc

111 create_db_and_tables() 1abc

112 create_heroes() 1abc

113 update_heroes() 1abc

114 

115 

116if __name__ == "__main__": 1abc

117 main()