Coverage for docs_src/tutorial/delete/tutorial001.py: 100%
69 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 1abcdef
3from sqlmodel import Field, Session, SQLModel, create_engine, select 1abcdef
6class Hero(SQLModel, table=True): 1abcdef
7 id: Optional[int] = Field(default=None, primary_key=True) 1abcdef
8 name: str = Field(index=True) 1abcdef
9 secret_name: str 1abcdef
10 age: Optional[int] = Field(default=None, index=True) 1abcdef
13sqlite_file_name = "database.db" 1abcdef
14sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcdef
16engine = create_engine(sqlite_url, echo=True) 1abcdef
19def create_db_and_tables(): 1abcdef
20 SQLModel.metadata.create_all(engine) 1abcdef
23def create_heroes(): 1abcdef
24 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1abcdef
25 hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") 1abcdef
26 hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) 1abcdef
27 hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) 1abcdef
28 hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) 1abcdef
29 hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) 1abcdef
30 hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) 1abcdef
32 with Session(engine) as session: 1abcdef
33 session.add(hero_1) 1abcdef
34 session.add(hero_2) 1abcdef
35 session.add(hero_3) 1abcdef
36 session.add(hero_4) 1abcdef
37 session.add(hero_5) 1abcdef
38 session.add(hero_6) 1abcdef
39 session.add(hero_7) 1abcdef
41 session.commit() 1abcdef
44def update_heroes(): 1abcdef
45 with Session(engine) as session: 1abcdef
46 statement = select(Hero).where(Hero.name == "Spider-Boy") 1abcdef
47 results = session.exec(statement) 1abcdef
48 hero_1 = results.one() 1abcdef
49 print("Hero 1:", hero_1) 1abcdef
51 statement = select(Hero).where(Hero.name == "Captain North America") 1abcdef
52 results = session.exec(statement) 1abcdef
53 hero_2 = results.one() 1abcdef
54 print("Hero 2:", hero_2) 1abcdef
56 hero_1.age = 16 1abcdef
57 hero_1.name = "Spider-Youngster" 1abcdef
58 session.add(hero_1) 1abcdef
60 hero_2.name = "Captain North America Except Canada" 1abcdef
61 hero_2.age = 110 1abcdef
62 session.add(hero_2) 1abcdef
64 session.commit() 1abcdef
65 session.refresh(hero_1) 1abcdef
66 session.refresh(hero_2) 1abcdef
68 print("Updated hero 1:", hero_1) 1abcdef
69 print("Updated hero 2:", hero_2) 1abcdef
72def delete_heroes(): 1abcdef
73 with Session(engine) as session: 1abcdef
74 statement = select(Hero).where(Hero.name == "Spider-Youngster") 1abcdef
75 results = session.exec(statement) 1abcdef
76 hero = results.one() 1abcdef
77 print("Hero: ", hero) 1abcdef
79 session.delete(hero) 1abcdef
80 session.commit() 1abcdef
82 print("Deleted hero:", hero) 1abcdef
84 statement = select(Hero).where(Hero.name == "Spider-Youngster") 1abcdef
85 results = session.exec(statement) 1abcdef
86 hero = results.first() 1abcdef
88 if hero is None: 1abcdef
89 print("There's no hero named Spider-Youngster") 1abcdef
92def main(): 1abcdef
93 create_db_and_tables() 1abcdef
94 create_heroes() 1abcdef
95 update_heroes() 1abcdef
96 delete_heroes() 1abcdef
99if __name__ == "__main__": 1abcdef
100 main()