Files
sqlmodel/tests/test_future_annotations.py
Victor Mota 66533c96d9 🐛 Fix support for Annotated fields with Pydantic 2.12+ (#1607)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
Co-authored-by: svlandeg <svlandeg@github.com>
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
2026-02-01 19:14:55 +01:00

64 lines
1.8 KiB
Python

from __future__ import annotations
from typing import Annotated, Optional
from sqlmodel import Field, Session, SQLModel, create_engine, select
def test_model_with_future_annotations(clear_sqlmodel):
class Hero(SQLModel, table=True):
id: Annotated[Optional[int], Field(primary_key=True)] = None
name: str
secret_name: str
age: Optional[int] = None
hero = Hero(name="Deadpond", secret_name="Dive Wilson", age=25)
engine = create_engine("sqlite://")
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
session.add(hero)
session.commit()
session.refresh(hero)
assert hero.id is not None
assert hero.name == "Deadpond"
assert hero.secret_name == "Dive Wilson"
assert hero.age == 25
with Session(engine) as session:
heroes = session.exec(select(Hero)).all()
assert len(heroes) == 1
assert heroes[0].name == "Deadpond"
def test_model_with_string_annotations(clear_sqlmodel):
class Team(SQLModel, table=True):
id: Annotated[Optional[int], Field(primary_key=True)] = None
name: str
class Player(SQLModel, table=True):
id: Annotated[Optional[int], Field(primary_key=True)] = None
name: str
team_id: Annotated[Optional[int], Field(foreign_key="team.id")] = None
engine = create_engine("sqlite://")
SQLModel.metadata.create_all(engine)
team = Team(name="Champions")
player = Player(name="Alice", team_id=None)
with Session(engine) as session:
session.add(team)
session.commit()
session.refresh(team)
player.team_id = team.id
session.add(player)
session.commit()
session.refresh(player)
assert team.id is not None
assert player.team_id == team.id