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

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

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

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

7 headquarters: str 1abc

8 

9 heroes: list["Hero"] = Relationship(back_populates="team") 1abc

10 

11 

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

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

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

15 secret_name: str 1abc

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

17 

18 team_id: int | None = Field( 1abc

19 default=None, foreign_key="team.id", ondelete="SET NULL" 

20 ) 

21 team: Team | None = Relationship(back_populates="heroes") 1abc

22 

23 

24sqlite_file_name = "database.db" 1abc

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

26 

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

28 

29 

30def create_db_and_tables(): 1abc

31 SQLModel.metadata.create_all(engine) 1abc

32 

33 

34def create_heroes(): 1abc

35 with Session(engine) as session: 1abc

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

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

38 

39 hero_deadpond = Hero( 1abc

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

41 ) 

42 hero_rusty_man = Hero( 1abc

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

44 ) 

45 hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") 1abc

46 session.add(hero_deadpond) 1abc

47 session.add(hero_rusty_man) 1abc

48 session.add(hero_spider_boy) 1abc

49 session.commit() 1abc

50 

51 session.refresh(hero_deadpond) 1abc

52 session.refresh(hero_rusty_man) 1abc

53 session.refresh(hero_spider_boy) 1abc

54 

55 print("Created hero:", hero_deadpond) 1abc

56 print("Created hero:", hero_rusty_man) 1abc

57 print("Created hero:", hero_spider_boy) 1abc

58 

59 hero_spider_boy.team = team_preventers 1abc

60 session.add(hero_spider_boy) 1abc

61 session.commit() 1abc

62 session.refresh(hero_spider_boy) 1abc

63 print("Updated hero:", hero_spider_boy) 1abc

64 

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

66 hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") 1abc

67 team_wakaland = Team( 1abc

68 name="Wakaland", 

69 headquarters="Wakaland Capital City", 

70 heroes=[hero_black_lion, hero_sure_e], 

71 ) 

72 session.add(team_wakaland) 1abc

73 session.commit() 1abc

74 session.refresh(team_wakaland) 1abc

75 print("Team Wakaland:", team_wakaland) 1abc

76 

77 

78def delete_team(): 1abc

79 with Session(engine) as session: 1abc

80 statement = select(Team).where(Team.name == "Wakaland") 1abc

81 team = session.exec(statement).one() 1abc

82 session.delete(team) 1abc

83 session.commit() 1abc

84 print("Deleted team:", team) 1abc

85 

86 

87def select_deleted_heroes(): 1abc

88 with Session(engine) as session: 1abc

89 statement = select(Hero).where(Hero.name == "Black Lion") 1abc

90 result = session.exec(statement) 1abc

91 hero = result.first() 1abc

92 print("Black Lion has no team:", hero) 1abc

93 

94 statement = select(Hero).where(Hero.name == "Princess Sure-E") 1abc

95 result = session.exec(statement) 1abc

96 hero = result.first() 1abc

97 print("Princess Sure-E has no team:", hero) 1abc

98 

99 

100def main(): 1abc

101 create_db_and_tables() 1abc

102 create_heroes() 1abc

103 delete_team() 1abc

104 select_deleted_heroes() 1abc

105 

106 

107if __name__ == "__main__": 1abc

108 main()