Coverage for docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py: 100%
83 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 typing import Optional 1abcd
3from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select 1abcd
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
11 heroes: list["Hero"] = Relationship(back_populates="team") 1abcd
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
20 team_id: Optional[int] = Field(default=None, foreign_key="team.id") 1abcd
21 team: Optional[Team] = Relationship(back_populates="heroes") 1abcd
24sqlite_file_name = "database.db" 1abcd
25sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcd
27engine = create_engine(sqlite_url, echo=True) 1abcd
30def create_db_and_tables(): 1abcd
31 SQLModel.metadata.create_all(engine) 1abcd
34def create_heroes(): 1abcd
35 with Session(engine) as session: 1abcd
36 team_preventers = Team(name="Preventers", headquarters="Sharp Tower") 1abcd
37 team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") 1abcd
39 hero_deadpond = Hero( 1abcd
40 name="Deadpond", secret_name="Dive Wilson", team=team_z_force
41 )
42 hero_rusty_man = Hero( 1abcd
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") 1abcd
46 session.add(hero_deadpond) 1abcd
47 session.add(hero_rusty_man) 1abcd
48 session.add(hero_spider_boy) 1abcd
49 session.commit() 1abcd
51 session.refresh(hero_deadpond) 1abcd
52 session.refresh(hero_rusty_man) 1abcd
53 session.refresh(hero_spider_boy) 1abcd
55 print("Created hero:", hero_deadpond) 1abcd
56 print("Created hero:", hero_rusty_man) 1abcd
57 print("Created hero:", hero_spider_boy) 1abcd
59 hero_spider_boy.team = team_preventers 1abcd
60 session.add(hero_spider_boy) 1abcd
61 session.commit() 1abcd
62 session.refresh(hero_spider_boy) 1abcd
63 print("Updated hero:", hero_spider_boy) 1abcd
65 hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) 1abcd
66 hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") 1abcd
67 team_wakaland = Team( 1abcd
68 name="Wakaland",
69 headquarters="Wakaland Capital City",
70 heroes=[hero_black_lion, hero_sure_e],
71 )
72 session.add(team_wakaland) 1abcd
73 session.commit() 1abcd
74 session.refresh(team_wakaland) 1abcd
75 print("Team Wakaland:", team_wakaland) 1abcd
77 hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) 1abcd
78 hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) 1abcd
79 hero_cap = Hero( 1abcd
80 name="Captain North America", secret_name="Esteban Rogelios", age=93
81 )
83 team_preventers.heroes.append(hero_tarantula) 1abcd
84 team_preventers.heroes.append(hero_dr_weird) 1abcd
85 team_preventers.heroes.append(hero_cap) 1abcd
86 session.add(team_preventers) 1abcd
87 session.commit() 1abcd
88 session.refresh(hero_tarantula) 1abcd
89 session.refresh(hero_dr_weird) 1abcd
90 session.refresh(hero_cap) 1abcd
91 print("Preventers new hero:", hero_tarantula) 1abcd
92 print("Preventers new hero:", hero_dr_weird) 1abcd
93 print("Preventers new hero:", hero_cap) 1abcd
96def select_heroes(): 1abcd
97 with Session(engine) as session: 1abcd
98 statement = select(Team).where(Team.name == "Preventers") 1abcd
99 result = session.exec(statement) 1abcd
100 team_preventers = result.one() 1abcd
102 print("Preventers heroes:", team_preventers.heroes) 1abcd
105def update_heroes(): 1abcd
106 with Session(engine) as session: 1abcd
107 statement = select(Hero).where(Hero.name == "Spider-Boy") 1abcd
108 result = session.exec(statement) 1abcd
109 hero_spider_boy = result.one() 1abcd
111 hero_spider_boy.team = None 1abcd
112 session.add(hero_spider_boy) 1abcd
113 session.commit() 1abcd
115 session.refresh(hero_spider_boy) 1abcd
116 print("Spider-Boy without team:", hero_spider_boy) 1abcd
119def main(): 1abcd
120 create_db_and_tables() 1abcd
121 create_heroes() 1abcd
122 select_heroes() 1abcd
123 update_heroes() 1abcd
126if __name__ == "__main__": 1abcd
127 main()