Coverage for docs_src/tutorial/connect/create_tables/tutorial001_py310.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1from sqlmodel import Field, SQLModel, create_engine 1abc

2 

3 

4class Team(SQLModel, table=True): 1abc

5 id: int | None = Field(default=None, primary_key=True) 1abc

6 name: str = Field(index=True) 1abc

7 headquarters: str 1abc

8 

9 

10class Hero(SQLModel, table=True): 1abc

11 id: int | None = Field(default=None, primary_key=True) 1abc

12 name: str = Field(index=True) 1abc

13 secret_name: str 1abc

14 age: int | None = Field(default=None, index=True) 1abc

15 

16 team_id: int | None = Field(default=None, foreign_key="team.id") 1abc

17 

18 

19sqlite_file_name = "database.db" 1abc

20sqlite_url = f"sqlite:///{sqlite_file_name}" 1abc

21 

22engine = create_engine(sqlite_url, echo=True) 1abc

23 

24 

25def create_db_and_tables(): 1abc

26 SQLModel.metadata.create_all(engine) 1abc

27 

28 

29def main(): 1abc

30 create_db_and_tables() 1abc

31 

32 

33if __name__ == "__main__": 1abc

34 main()