mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-08-14 17:41:37 +08:00
✨ Add source examples for docs
This commit is contained in:
0
docs_src/tutorial/code_structure/__init__.py
Normal file
0
docs_src/tutorial/code_structure/__init__.py
Normal file
29
docs_src/tutorial/code_structure/tutorial001/app.py
Normal file
29
docs_src/tutorial/code_structure/tutorial001/app.py
Normal file
@ -0,0 +1,29 @@
|
||||
from sqlmodel import Session
|
||||
|
||||
from .database import create_db_and_tables, engine
|
||||
from .models import Hero, Team
|
||||
|
||||
|
||||
def create_heroes():
|
||||
with Session(engine) as session:
|
||||
team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
|
||||
|
||||
hero_deadpond = Hero(
|
||||
name="Deadpond", secret_name="Dive Wilson", team=team_z_force
|
||||
)
|
||||
session.add(hero_deadpond)
|
||||
session.commit()
|
||||
|
||||
session.refresh(hero_deadpond)
|
||||
|
||||
print("Created hero:", hero_deadpond)
|
||||
print("Hero's team:", hero_deadpond.team)
|
||||
|
||||
|
||||
def main():
|
||||
create_db_and_tables()
|
||||
create_heroes()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
10
docs_src/tutorial/code_structure/tutorial001/database.py
Normal file
10
docs_src/tutorial/code_structure/tutorial001/database.py
Normal file
@ -0,0 +1,10 @@
|
||||
from sqlmodel import SQLModel, create_engine
|
||||
|
||||
sqlite_file_name = "database.db"
|
||||
sqlite_url = f"sqlite:///{sqlite_file_name}"
|
||||
|
||||
engine = create_engine(sqlite_url)
|
||||
|
||||
|
||||
def create_db_and_tables():
|
||||
SQLModel.metadata.create_all(engine)
|
21
docs_src/tutorial/code_structure/tutorial001/models.py
Normal file
21
docs_src/tutorial/code_structure/tutorial001/models.py
Normal file
@ -0,0 +1,21 @@
|
||||
from typing import List, Optional
|
||||
|
||||
from sqlmodel import Field, Relationship, SQLModel
|
||||
|
||||
|
||||
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")
|
30
docs_src/tutorial/code_structure/tutorial002/app.py
Normal file
30
docs_src/tutorial/code_structure/tutorial002/app.py
Normal file
@ -0,0 +1,30 @@
|
||||
from sqlmodel import Session
|
||||
|
||||
from .database import create_db_and_tables, engine
|
||||
from .hero_model import Hero
|
||||
from .team_model import Team
|
||||
|
||||
|
||||
def create_heroes():
|
||||
with Session(engine) as session:
|
||||
team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
|
||||
|
||||
hero_deadpond = Hero(
|
||||
name="Deadpond", secret_name="Dive Wilson", team=team_z_force
|
||||
)
|
||||
session.add(hero_deadpond)
|
||||
session.commit()
|
||||
|
||||
session.refresh(hero_deadpond)
|
||||
|
||||
print("Created hero:", hero_deadpond)
|
||||
print("Hero's team:", hero_deadpond.team)
|
||||
|
||||
|
||||
def main():
|
||||
create_db_and_tables()
|
||||
create_heroes()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
10
docs_src/tutorial/code_structure/tutorial002/database.py
Normal file
10
docs_src/tutorial/code_structure/tutorial002/database.py
Normal file
@ -0,0 +1,10 @@
|
||||
from sqlmodel import SQLModel, create_engine
|
||||
|
||||
sqlite_file_name = "database.db"
|
||||
sqlite_url = f"sqlite:///{sqlite_file_name}"
|
||||
|
||||
engine = create_engine(sqlite_url)
|
||||
|
||||
|
||||
def create_db_and_tables():
|
||||
SQLModel.metadata.create_all(engine)
|
16
docs_src/tutorial/code_structure/tutorial002/hero_model.py
Normal file
16
docs_src/tutorial/code_structure/tutorial002/hero_model.py
Normal file
@ -0,0 +1,16 @@
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
from sqlmodel import Field, Relationship, SQLModel
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .team_model import 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")
|
14
docs_src/tutorial/code_structure/tutorial002/team_model.py
Normal file
14
docs_src/tutorial/code_structure/tutorial002/team_model.py
Normal file
@ -0,0 +1,14 @@
|
||||
from typing import TYPE_CHECKING, List, Optional
|
||||
|
||||
from sqlmodel import Field, Relationship, SQLModel
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .hero_model import Hero
|
||||
|
||||
|
||||
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")
|
Reference in New Issue
Block a user