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

19 statements  

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

1from typing import Optional 1abcdef

2 

3from sqlmodel import Field, SQLModel, create_engine 1abcdef

4 

5 

6class Team(SQLModel, table=True): 1abcdef

7 id: Optional[int] = Field(default=None, primary_key=True) 1abcdef

8 name: str = Field(index=True) 1abcdef

9 headquarters: str 1abcdef

10 

11 

12class Hero(SQLModel, table=True): 1abcdef

13 id: Optional[int] = Field(default=None, primary_key=True) 1abcdef

14 name: str = Field(index=True) 1abcdef

15 secret_name: str 1abcdef

16 age: Optional[int] = Field(default=None, index=True) 1abcdef

17 

18 team_id: Optional[int] = Field(default=None, foreign_key="team.id") 1abcdef

19 

20 

21sqlite_file_name = "database.db" 1abcdef

22sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcdef

23 

24engine = create_engine(sqlite_url, echo=True) 1abcdef

25 

26 

27def create_db_and_tables(): 1abcdef

28 SQLModel.metadata.create_all(engine) 1abcdef

29 

30 

31def main(): 1abcdef

32 create_db_and_tables() 1abcdef

33 

34 

35if __name__ == "__main__": 1abcdef

36 main()