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

70 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-27 00:03 +0000

1from typing import List, Optional 1abcdefghij

2 

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

4 

5 

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

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

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

9 headquarters: str 1abcdefghij

10 

11 heroes: List["Hero"] = Relationship(back_populates="team", cascade_delete=True) 1abcdefghij

12 

13 

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

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

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

17 secret_name: str 1abcdefghij

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

19 

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

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

22 ) 

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

24 

25 

26sqlite_file_name = "database.db" 1abcdefghij

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

28 

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

30 

31 

32def create_db_and_tables(): 1abcdefghij

33 SQLModel.metadata.create_all(engine) 1abcdefghij

34 

35 

36def create_heroes(): 1abcdefghij

37 with Session(engine) as session: 1abcdefghij

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

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

40 

41 hero_deadpond = Hero( 1abcdefghij

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

43 ) 

44 hero_rusty_man = Hero( 1abcdefghij

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

46 ) 

47 hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") 1abcdefghij

48 session.add(hero_deadpond) 1abcdefghij

49 session.add(hero_rusty_man) 1abcdefghij

50 session.add(hero_spider_boy) 1abcdefghij

51 session.commit() 1abcdefghij

52 

53 session.refresh(hero_deadpond) 1abcdefghij

54 session.refresh(hero_rusty_man) 1abcdefghij

55 session.refresh(hero_spider_boy) 1abcdefghij

56 

57 print("Created hero:", hero_deadpond) 1abcdefghij

58 print("Created hero:", hero_rusty_man) 1abcdefghij

59 print("Created hero:", hero_spider_boy) 1abcdefghij

60 

61 hero_spider_boy.team = team_preventers 1abcdefghij

62 session.add(hero_spider_boy) 1abcdefghij

63 session.commit() 1abcdefghij

64 session.refresh(hero_spider_boy) 1abcdefghij

65 print("Updated hero:", hero_spider_boy) 1abcdefghij

66 

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

68 hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") 1abcdefghij

69 team_wakaland = Team( 1abcdefghij

70 name="Wakaland", 

71 headquarters="Wakaland Capital City", 

72 heroes=[hero_black_lion, hero_sure_e], 

73 ) 

74 session.add(team_wakaland) 1abcdefghij

75 session.commit() 1abcdefghij

76 session.refresh(team_wakaland) 1abcdefghij

77 print("Team Wakaland:", team_wakaland) 1abcdefghij

78 

79 

80def delete_team(): 1abcdefghij

81 with Session(engine) as session: 1abcdefghij

82 statement = select(Team).where(Team.name == "Wakaland") 1abcdefghij

83 team = session.exec(statement).one() 1abcdefghij

84 session.delete(team) 1abcdefghij

85 session.commit() 1abcdefghij

86 print("Deleted team:", team) 1abcdefghij

87 

88 

89def select_deleted_heroes(): 1abcdefghij

90 with Session(engine) as session: 1abcdefghij

91 statement = select(Hero).where(Hero.name == "Black Lion") 1abcdefghij

92 result = session.exec(statement) 1abcdefghij

93 hero = result.first() 1abcdefghij

94 print("Black Lion not found:", hero) 1abcdefghij

95 

96 statement = select(Hero).where(Hero.name == "Princess Sure-E") 1abcdefghij

97 result = session.exec(statement) 1abcdefghij

98 hero = result.first() 1abcdefghij

99 print("Princess Sure-E not found:", hero) 1abcdefghij

100 

101 

102def main(): 1abcdefghij

103 create_db_and_tables() 1abcdefghij

104 create_heroes() 1abcdefghij

105 delete_team() 1abcdefghij

106 select_deleted_heroes() 1abcdefghij

107 

108 

109if __name__ == "__main__": 1abcdefghij

110 main()