Coverage for docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005.py: 100%

82 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, text 1abcdef

4 

5 

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

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

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

9 headquarters: str 1abcdef

10 

11 heroes: List["Hero"] = Relationship(back_populates="team", passive_deletes="all") 1abcdef

12 

13 

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

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

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

17 secret_name: str 1abcdef

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

19 

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

21 default=None, foreign_key="team.id", ondelete="RESTRICT" 

22 ) 

23 team: Optional[Team] = Relationship(back_populates="heroes") 1abcdef

24 

25 

26sqlite_file_name = "database.db" 1abcdef

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

28 

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

30 

31 

32def create_db_and_tables(): 1abcdef

33 SQLModel.metadata.create_all(engine) 1abcdef

34 with engine.connect() as connection: 1abcdef

35 connection.execute(text("PRAGMA foreign_keys=ON")) # for SQLite only 1abcdef

36 

37 

38def create_heroes(): 1abcdef

39 with Session(engine) as session: 1abcdef

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

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

42 

43 hero_deadpond = Hero( 1abcdef

44 name="Deadpond", secret_name="Dive Wilson", team=team_z_force 

45 ) 

46 hero_rusty_man = Hero( 1abcdef

47 name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers 

48 ) 

49 hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") 1abcdef

50 session.add(hero_deadpond) 1abcdef

51 session.add(hero_rusty_man) 1abcdef

52 session.add(hero_spider_boy) 1abcdef

53 session.commit() 1abcdef

54 

55 session.refresh(hero_deadpond) 1abcdef

56 session.refresh(hero_rusty_man) 1abcdef

57 session.refresh(hero_spider_boy) 1abcdef

58 

59 print("Created hero:", hero_deadpond) 1abcdef

60 print("Created hero:", hero_rusty_man) 1abcdef

61 print("Created hero:", hero_spider_boy) 1abcdef

62 

63 hero_spider_boy.team = team_preventers 1abcdef

64 session.add(hero_spider_boy) 1abcdef

65 session.commit() 1abcdef

66 session.refresh(hero_spider_boy) 1abcdef

67 print("Updated hero:", hero_spider_boy) 1abcdef

68 

69 hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) 1abcdef

70 hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") 1abcdef

71 team_wakaland = Team( 1abcdef

72 name="Wakaland", 

73 headquarters="Wakaland Capital City", 

74 heroes=[hero_black_lion, hero_sure_e], 

75 ) 

76 session.add(team_wakaland) 1abcdef

77 session.commit() 1abcdef

78 session.refresh(team_wakaland) 1abcdef

79 print("Team Wakaland:", team_wakaland) 1abcdef

80 

81 

82def remove_team_heroes(): 1abcdef

83 with Session(engine) as session: 1abcdef

84 statement = select(Team).where(Team.name == "Wakaland") 1abcdef

85 team = session.exec(statement).one() 1abcdef

86 team.heroes.clear() 1abcdef

87 session.add(team) 1abcdef

88 session.commit() 1abcdef

89 session.refresh(team) 1abcdef

90 print("Team with removed heroes:", team) 1abcdef

91 

92 

93def delete_team(): 1abcdef

94 with Session(engine) as session: 1abcdef

95 statement = select(Team).where(Team.name == "Wakaland") 1abcdef

96 team = session.exec(statement).one() 1abcdef

97 session.delete(team) 1abcdef

98 session.commit() 1abcdef

99 print("Deleted team:", team) 1abcdef

100 

101 

102def select_deleted_heroes(): 1abcdef

103 with Session(engine) as session: 1abcdef

104 statement = select(Hero).where(Hero.name == "Black Lion") 1abcdef

105 result = session.exec(statement) 1abcdef

106 hero = result.first() 1abcdef

107 print("Black Lion has no team:", hero) 1abcdef

108 

109 statement = select(Hero).where(Hero.name == "Princess Sure-E") 1abcdef

110 result = session.exec(statement) 1abcdef

111 hero = result.first() 1abcdef

112 print("Princess Sure-E has no team:", hero) 1abcdef

113 

114 

115def main(): 1abcdef

116 create_db_and_tables() 1abcdef

117 create_heroes() 1abcdef

118 remove_team_heroes() 1abcdef

119 delete_team() 1abcdef

120 select_deleted_heroes() 1abcdef

121 

122 

123if __name__ == "__main__": 1abcdef

124 main()