mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-08-15 02:07:54 +08:00
📝 Add source examples for Python 3.9 and 3.10 (#715)
* 📝 Add source examples for Python 3.9 and 3.10 * ✅ Add tests for new source examples for Python 3.9 and 3.10, still needs pytest markers * ✅ Add tests for fastapi examples * ✅ Update tests for FastAPI app testing, for Python 3.9 and 3.10, fixing multi-app testing conflicts * ✅ Require Python 3.9 and 3.10 for tests * ✅ Update tests with missing markers
This commit is contained in:

committed by
GitHub

parent
cce30d7546
commit
d8effcbc5c
30
docs_src/tutorial/code_structure/tutorial002_py310/app.py
Normal file
30
docs_src/tutorial/code_structure/tutorial002_py310/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()
|
@ -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)
|
@ -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: int | None = Field(default=None, primary_key=True)
|
||||
name: str = Field(index=True)
|
||||
secret_name: str
|
||||
age: int | None = Field(default=None, index=True)
|
||||
|
||||
team_id: int | None = Field(default=None, foreign_key="team.id")
|
||||
team: Optional["Team"] = Relationship(back_populates="heroes")
|
@ -0,0 +1,14 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlmodel import Field, Relationship, SQLModel
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .hero_model import Hero
|
||||
|
||||
|
||||
class Team(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
name: str = Field(index=True)
|
||||
headquarters: str
|
||||
|
||||
heroes: list["Hero"] = Relationship(back_populates="team")
|
Reference in New Issue
Block a user