mirror of
https://github.com/PaiGramTeam/sqlmodel.git
synced 2024-11-30 02:44:13 +00:00
82 lines
2.2 KiB
Python
82 lines
2.2 KiB
Python
from typing import Optional
|
||
|
||
from sqlmodel import Field, Session, SQLModel, create_engine
|
||
|
||
|
||
class Team(SQLModel, table=True):
|
||
id: Optional[int] = Field(default=None, primary_key=True)
|
||
name: str
|
||
headquarters: str
|
||
|
||
|
||
class Hero(SQLModel, table=True):
|
||
id: Optional[int] = Field(default=None, primary_key=True)
|
||
name: str
|
||
secret_name: str
|
||
age: Optional[int] = None
|
||
|
||
team_id: Optional[int] = Field(default=None, foreign_key="team.id")
|
||
|
||
|
||
sqlite_file_name = "database.db"
|
||
sqlite_url = f"sqlite:///{sqlite_file_name}"
|
||
|
||
engine = create_engine(sqlite_url, echo=True)
|
||
|
||
|
||
def create_db_and_tables():
|
||
SQLModel.metadata.create_all(engine)
|
||
|
||
|
||
def create_heroes():
|
||
with Session(engine) as session:
|
||
team_preventers = Team(name="Preventers", headquarters="Sharp Tower")
|
||
team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
|
||
session.add(team_preventers)
|
||
session.add(team_z_force)
|
||
session.commit()
|
||
|
||
hero_deadpond = Hero(
|
||
name="Deadpond", secret_name="Dive Wilson", team_id=team_z_force.id
|
||
)
|
||
hero_rusty_man = Hero(
|
||
name="Rusty-Man",
|
||
secret_name="Tommy Sharp",
|
||
age=48,
|
||
team_id=team_preventers.id,
|
||
)
|
||
hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
|
||
session.add(hero_deadpond)
|
||
session.add(hero_rusty_man)
|
||
session.add(hero_spider_boy)
|
||
session.commit()
|
||
|
||
session.refresh(hero_deadpond)
|
||
session.refresh(hero_rusty_man)
|
||
session.refresh(hero_spider_boy)
|
||
|
||
print("Created hero:", hero_deadpond)
|
||
print("Created hero:", hero_rusty_man)
|
||
print("Created hero:", hero_spider_boy)
|
||
|
||
hero_spider_boy.team_id = team_preventers.id
|
||
session.add(hero_spider_boy)
|
||
session.commit()
|
||
session.refresh(hero_spider_boy)
|
||
print("Updated hero:", hero_spider_boy)
|
||
|
||
hero_spider_boy.team_id = None
|
||
session.add(hero_spider_boy)
|
||
session.commit()
|
||
session.refresh(hero_spider_boy)
|
||
print("No longer Preventer:", hero_spider_boy)
|
||
|
||
|
||
def main():
|
||
create_db_and_tables()
|
||
create_heroes()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|