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

82 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1from typing import Optional 1abcd

2 

3from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select, text 1abcd

4 

5 

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

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

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

9 headquarters: str 1abcd

10 

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

12 

13 

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

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

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

17 secret_name: str 1abcd

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

19 

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

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

22 ) 

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

24 

25 

26sqlite_file_name = "database.db" 1abcd

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

28 

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

30 

31 

32def create_db_and_tables(): 1abcd

33 SQLModel.metadata.create_all(engine) 1abcd

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

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

36 

37 

38def create_heroes(): 1abcd

39 with Session(engine) as session: 1abcd

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

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

42 

43 hero_deadpond = Hero( 1abcd

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

45 ) 

46 hero_rusty_man = Hero( 1abcd

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") 1abcd

50 session.add(hero_deadpond) 1abcd

51 session.add(hero_rusty_man) 1abcd

52 session.add(hero_spider_boy) 1abcd

53 session.commit() 1abcd

54 

55 session.refresh(hero_deadpond) 1abcd

56 session.refresh(hero_rusty_man) 1abcd

57 session.refresh(hero_spider_boy) 1abcd

58 

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

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

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

62 

63 hero_spider_boy.team = team_preventers 1abcd

64 session.add(hero_spider_boy) 1abcd

65 session.commit() 1abcd

66 session.refresh(hero_spider_boy) 1abcd

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

68 

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

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

71 team_wakaland = Team( 1abcd

72 name="Wakaland", 

73 headquarters="Wakaland Capital City", 

74 heroes=[hero_black_lion, hero_sure_e], 

75 ) 

76 session.add(team_wakaland) 1abcd

77 session.commit() 1abcd

78 session.refresh(team_wakaland) 1abcd

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

80 

81 

82def remove_team_heroes(): 1abcd

83 with Session(engine) as session: 1abcd

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

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

86 team.heroes.clear() 1abcd

87 session.add(team) 1abcd

88 session.commit() 1abcd

89 session.refresh(team) 1abcd

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

91 

92 

93def delete_team(): 1abcd

94 with Session(engine) as session: 1abcd

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

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

97 session.delete(team) 1abcd

98 session.commit() 1abcd

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

100 

101 

102def select_deleted_heroes(): 1abcd

103 with Session(engine) as session: 1abcd

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

105 result = session.exec(statement) 1abcd

106 hero = result.first() 1abcd

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

108 

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

110 result = session.exec(statement) 1abcd

111 hero = result.first() 1abcd

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

113 

114 

115def main(): 1abcd

116 create_db_and_tables() 1abcd

117 create_heroes() 1abcd

118 remove_team_heroes() 1abcd

119 delete_team() 1abcd

120 select_deleted_heroes() 1abcd

121 

122 

123if __name__ == "__main__": 1abcd

124 main()