mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-08-15 02:07:54 +08:00
✨ Add source examples for docs
This commit is contained in:
0
docs_src/tutorial/create_db_and_table/__init__.py
Normal file
0
docs_src/tutorial/create_db_and_table/__init__.py
Normal file
18
docs_src/tutorial/create_db_and_table/tutorial001.py
Normal file
18
docs_src/tutorial/create_db_and_table/tutorial001.py
Normal file
@ -0,0 +1,18 @@
|
||||
from typing import Optional
|
||||
|
||||
from sqlmodel import Field, SQLModel, create_engine
|
||||
|
||||
|
||||
class Hero(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
name: str
|
||||
secret_name: str
|
||||
age: Optional[int] = None
|
||||
|
||||
|
||||
sqlite_file_name = "database.db"
|
||||
sqlite_url = f"sqlite:///{sqlite_file_name}"
|
||||
|
||||
engine = create_engine(sqlite_url, echo=True)
|
||||
|
||||
SQLModel.metadata.create_all(engine)
|
24
docs_src/tutorial/create_db_and_table/tutorial002.py
Normal file
24
docs_src/tutorial/create_db_and_table/tutorial002.py
Normal file
@ -0,0 +1,24 @@
|
||||
from typing import Optional
|
||||
|
||||
from sqlmodel import Field, SQLModel, create_engine
|
||||
|
||||
|
||||
class Hero(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
name: str
|
||||
secret_name: str
|
||||
age: Optional[int] = None
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_db_and_tables()
|
24
docs_src/tutorial/create_db_and_table/tutorial003.py
Normal file
24
docs_src/tutorial/create_db_and_table/tutorial003.py
Normal file
@ -0,0 +1,24 @@
|
||||
from typing import Optional # (1)
|
||||
|
||||
from sqlmodel import Field, SQLModel, create_engine # (2)
|
||||
|
||||
|
||||
class Hero(SQLModel, table=True): # (3)
|
||||
id: Optional[int] = Field(default=None, primary_key=True) # (4)
|
||||
name: str # (5)
|
||||
secret_name: str # (6)
|
||||
age: Optional[int] = None # (7)
|
||||
|
||||
|
||||
sqlite_file_name = "database.db" # (8)
|
||||
sqlite_url = f"sqlite:///{sqlite_file_name}" # (9)
|
||||
|
||||
engine = create_engine(sqlite_url, echo=True) # (10)
|
||||
|
||||
|
||||
def create_db_and_tables(): # (11)
|
||||
SQLModel.metadata.create_all(engine) # (12)
|
||||
|
||||
|
||||
if __name__ == "__main__": # (13)
|
||||
create_db_and_tables() # (14)
|
Reference in New Issue
Block a user