Coverage for docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py310.py: 100%
81 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
« 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, text 1abc
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
9 heroes: list["Hero"] = Relationship(back_populates="team", passive_deletes="all") 1abc
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
18 team_id: int | None = Field( 1abc
19 default=None, foreign_key="team.id", ondelete="RESTRICT"
20 )
21 team: Team | None = Relationship(back_populates="heroes") 1abc
24sqlite_file_name = "database.db" 1abc
25sqlite_url = f"sqlite:///{sqlite_file_name}" 1abc
27engine = create_engine(sqlite_url, echo=True) 1abc
30def create_db_and_tables(): 1abc
31 SQLModel.metadata.create_all(engine) 1abc
32 with engine.connect() as connection: 1abc
33 connection.execute(text("PRAGMA foreign_keys=ON")) # for SQLite only 1abc
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
41 hero_deadpond = Hero( 1abc
42 name="Deadpond", secret_name="Dive Wilson", team=team_z_force
43 )
44 hero_rusty_man = Hero( 1abc
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") 1abc
48 session.add(hero_deadpond) 1abc
49 session.add(hero_rusty_man) 1abc
50 session.add(hero_spider_boy) 1abc
51 session.commit() 1abc
53 session.refresh(hero_deadpond) 1abc
54 session.refresh(hero_rusty_man) 1abc
55 session.refresh(hero_spider_boy) 1abc
57 print("Created hero:", hero_deadpond) 1abc
58 print("Created hero:", hero_rusty_man) 1abc
59 print("Created hero:", hero_spider_boy) 1abc
61 hero_spider_boy.team = team_preventers 1abc
62 session.add(hero_spider_boy) 1abc
63 session.commit() 1abc
64 session.refresh(hero_spider_boy) 1abc
65 print("Updated hero:", hero_spider_boy) 1abc
67 hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) 1abc
68 hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") 1abc
69 team_wakaland = Team( 1abc
70 name="Wakaland",
71 headquarters="Wakaland Capital City",
72 heroes=[hero_black_lion, hero_sure_e],
73 )
74 session.add(team_wakaland) 1abc
75 session.commit() 1abc
76 session.refresh(team_wakaland) 1abc
77 print("Team Wakaland:", team_wakaland) 1abc
80def remove_team_heroes(): 1abc
81 with Session(engine) as session: 1abc
82 statement = select(Team).where(Team.name == "Wakaland") 1abc
83 team = session.exec(statement).one() 1abc
84 team.heroes.clear() 1abc
85 session.add(team) 1abc
86 session.commit() 1abc
87 session.refresh(team) 1abc
88 print("Team with removed heroes:", team) 1abc
91def delete_team(): 1abc
92 with Session(engine) as session: 1abc
93 statement = select(Team).where(Team.name == "Wakaland") 1abc
94 team = session.exec(statement).one() 1abc
95 session.delete(team) 1abc
96 session.commit() 1abc
97 print("Deleted team:", team) 1abc
100def select_deleted_heroes(): 1abc
101 with Session(engine) as session: 1abc
102 statement = select(Hero).where(Hero.name == "Black Lion") 1abc
103 result = session.exec(statement) 1abc
104 hero = result.first() 1abc
105 print("Black Lion has no team:", hero) 1abc
107 statement = select(Hero).where(Hero.name == "Princess Sure-E") 1abc
108 result = session.exec(statement) 1abc
109 hero = result.first() 1abc
110 print("Princess Sure-E has no team:", hero) 1abc
113def main(): 1abc
114 create_db_and_tables() 1abc
115 create_heroes() 1abc
116 remove_team_heroes() 1abc
117 delete_team() 1abc
118 select_deleted_heroes() 1abc
121if __name__ == "__main__": 1abc
122 main()