from typing import List, Optional

from sqlmodel import Field, Relationship, SQLModel, create_engine


class Weapon(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str

    hero: "Hero" = Relationship(back_populates="weapon")


class Power(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str

    hero_id: int = Field(foreign_key="hero.id")
    hero: "Hero" = Relationship(back_populates="powers")


class Team(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    headquarters: str

    heroes: List["Hero"] = Relationship(back_populates="team")


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")
    team: Optional[Team] = Relationship(back_populates="heroes")

    weapon_id: Optional[int] = Field(default=None, foreign_key="weapon.id")
    weapon: Optional[Weapon] = Relationship(back_populates="owner")

    powers: List[Power] = Relationship(back_populates="hero")


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 main():
    create_db_and_tables()


if __name__ == "__main__":
    main()

sqlmodel.Relationship sqlmodel.SQLModel.metadata.create_all sqlmodel.create_engine sqlmodel.Field
[((1289, 1325), 'sqlmodel.create_engine', 'create_engine', (['sqlite_url'], {'echo': '(True)'}), '(sqlite_url, echo=True)\n', (1302, 1325), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((163, 200), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (168, 200), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((235, 272), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""weapon"""'}), "(back_populates='weapon')\n", (247, 272), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((334, 371), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (339, 371), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((406, 434), 'sqlmodel.Field', 'Field', ([], {'foreign_key': '"""hero.id"""'}), "(foreign_key='hero.id')\n", (411, 434), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((454, 491), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""powers"""'}), "(back_populates='powers')\n", (466, 491), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((552, 589), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (557, 589), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((654, 689), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""team"""'}), "(back_populates='team')\n", (666, 689), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((750, 787), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (755, 787), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((883, 925), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""team.id"""'}), "(default=None, foreign_key='team.id')\n", (888, 925), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((953, 990), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""heroes"""'}), "(back_populates='heroes')\n", (965, 990), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((1023, 1067), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""weapon.id"""'}), "(default=None, foreign_key='weapon.id')\n", (1028, 1067), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((1099, 1135), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""owner"""'}), "(back_populates='owner')\n", (1111, 1135), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((1163, 1198), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""hero"""'}), "(back_populates='hero')\n", (1175, 1198), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n'), ((1360, 1396), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (1388, 1396), False, 'from sqlmodel import Field, Relationship, SQLModel, create_engine\n')]