MCPcopy Index your code
hub / github.com/fastapi/sqlmodel / test_model_with_string_annotations

Function test_model_with_string_annotations

tests/test_future_annotations.py:36–63  ·  view source on GitHub ↗
(clear_sqlmodel)

Source from the content-addressed store, hash-verified

34
35
36def test_model_with_string_annotations(clear_sqlmodel):
37 class Team(SQLModel, table=True):
38 id: Annotated[int | None, Field(primary_key=True)] = None
39 name: str
40
41 class Player(SQLModel, table=True):
42 id: Annotated[int | None, Field(primary_key=True)] = None
43 name: str
44 team_id: Annotated[int | None, Field(foreign_key="team.id")] = None
45
46 engine = create_engine("sqlite://")
47 SQLModel.metadata.create_all(engine)
48
49 team = Team(name="Champions")
50 player = Player(name="Alice", team_id=None)
51
52 with Session(engine) as session:
53 session.add(team)
54 session.commit()
55 session.refresh(team)
56
57 player.team_id = team.id
58 session.add(player)
59 session.commit()
60 session.refresh(player)
61
62 assert team.id is not None
63 assert player.team_id == team.id

Callers

nothing calls this directly

Calls 3

SessionClass · 0.90
PlayerClass · 0.85
TeamClass · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…