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

58 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 

8 

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

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

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

12 headquarters: str 1abc

13 

14 heroes: list["Hero"] = Relationship(back_populates="teams", link_model=HeroTeamLink) 1abc

15 

16 

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

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

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

20 secret_name: str 1abc

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

22 

23 teams: list[Team] = Relationship(back_populates="heroes", link_model=HeroTeamLink) 1abc

24 

25 

26sqlite_file_name = "database.db" 1abc

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

28 

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

30 

31 

32def create_db_and_tables(): 1abc

33 SQLModel.metadata.create_all(engine) 1abc

34 

35 

36def create_heroes(): 1abc

37 with Session(engine) as session: 1abc

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

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

40 

41 hero_deadpond = Hero( 1abc

42 name="Deadpond", 

43 secret_name="Dive Wilson", 

44 teams=[team_z_force, team_preventers], 

45 ) 

46 hero_rusty_man = Hero( 1abc

47 name="Rusty-Man", 

48 secret_name="Tommy Sharp", 

49 age=48, 

50 teams=[team_preventers], 

51 ) 

52 hero_spider_boy = Hero( 1abc

53 name="Spider-Boy", secret_name="Pedro Parqueador", teams=[team_preventers] 

54 ) 

55 session.add(hero_deadpond) 1abc

56 session.add(hero_rusty_man) 1abc

57 session.add(hero_spider_boy) 1abc

58 session.commit() 1abc

59 

60 session.refresh(hero_deadpond) 1abc

61 session.refresh(hero_rusty_man) 1abc

62 session.refresh(hero_spider_boy) 1abc

63 

64 print("Deadpond:", hero_deadpond) 1abc

65 print("Deadpond teams:", hero_deadpond.teams) 1abc

66 print("Rusty-Man:", hero_rusty_man) 1abc

67 print("Rusty-Man Teams:", hero_rusty_man.teams) 1abc

68 print("Spider-Boy:", hero_spider_boy) 1abc

69 print("Spider-Boy Teams:", hero_spider_boy.teams) 1abc

70 

71 

72def update_heroes(): 1abc

73 with Session(engine) as session: 1abc

74 hero_spider_boy = session.exec( 1abc

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

76 ).one() 

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

78 

79 team_z_force.heroes.append(hero_spider_boy) 1abc

80 session.add(team_z_force) 1abc

81 session.commit() 1abc

82 

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

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

85 

86 hero_spider_boy.teams.remove(team_z_force) 1abc

87 session.add(team_z_force) 1abc

88 session.commit() 1abc

89 

90 print("Reverted Z-Force's heroes:", team_z_force.heroes) 1abc

91 print("Reverted Spider-Boy's teams:", hero_spider_boy.teams) 1abc

92 

93 

94def main(): 1abc

95 create_db_and_tables() 1abc

96 create_heroes() 1abc

97 update_heroes() 1abc

98 

99 

100if __name__ == "__main__": 1abc

101 main()