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