mirror of
https://github.com/PaiGramTeam/sqlmodel.git
synced 2024-11-30 10:53:07 +00:00
37 lines
730 B
Python
37 lines
730 B
Python
|
from typing import Optional
|
||
|
|
||
|
from sqlmodel import Field, 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 main():
|
||
|
create_db_and_tables()
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|