Coverage for docs_src / tutorial / relationship_attributes / back_populates / tutorial003_py310.py: 100%
32 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-01-06 21:09 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-01-06 21:09 +0000
1from sqlmodel import Field, Relationship, SQLModel, create_engine 1abcdefgh
4class Weapon(SQLModel, table=True): 1abcdefgh
5 id: int | None = Field(default=None, primary_key=True) 1abcdefgh
6 name: str = Field(index=True) 1abcdefgh
8 hero: "Hero" = Relationship(back_populates="weapon") 1abcdefgh
11class Power(SQLModel, table=True): 1abcdefgh
12 id: int | None = Field(default=None, primary_key=True) 1abcdefgh
13 name: str = Field(index=True) 1abcdefgh
15 hero_id: int = Field(foreign_key="hero.id") 1abcdefgh
16 hero: "Hero" = Relationship(back_populates="powers") 1abcdefgh
19class Team(SQLModel, table=True): 1abcdefgh
20 id: int | None = Field(default=None, primary_key=True) 1abcdefgh
21 name: str = Field(index=True) 1abcdefgh
22 headquarters: str 1abcdefgh
24 heroes: list["Hero"] = Relationship(back_populates="team") 1abcdefgh
27class Hero(SQLModel, table=True): 1abcdefgh
28 id: int | None = Field(default=None, primary_key=True) 1abcdefgh
29 name: str = Field(index=True) 1abcdefgh
30 secret_name: str 1abcdefgh
31 age: int | None = Field(default=None, index=True) 1abcdefgh
33 team_id: int | None = Field(default=None, foreign_key="team.id") 1abcdefgh
34 team: Team | None = Relationship(back_populates="heroes") 1abcdefgh
36 weapon_id: int | None = Field(default=None, foreign_key="weapon.id") 1abcdefgh
37 weapon: Weapon | None = Relationship(back_populates="hero") 1abcdefgh
39 powers: list[Power] = Relationship(back_populates="hero") 1abcdefgh
42sqlite_file_name = "database.db" 1abcdefgh
43sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcdefgh
45engine = create_engine(sqlite_url, echo=True) 1abcdefgh
48def create_db_and_tables(): 1abcdefgh
49 SQLModel.metadata.create_all(engine) 1ijklmnop
52def main(): 1abcdefgh
53 create_db_and_tables() 1ijklmnop
56if __name__ == "__main__": 1abcdefgh
57 main()