mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-10-27 19:47:12 +08:00
🐛 Fix support for types with Optional[Annoated[x, f()]], e.g. id: Optional[pydantic.UUID4] (#1093)
This commit is contained in:
committed by
GitHub
parent
4eaf8b9efb
commit
a14ab0bd3c
26
tests/test_annotated_uuid.py
Normal file
26
tests/test_annotated_uuid.py
Normal file
@ -0,0 +1,26 @@
|
||||
import uuid
|
||||
from typing import Optional
|
||||
|
||||
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
||||
|
||||
from tests.conftest import needs_pydanticv2
|
||||
|
||||
|
||||
@needs_pydanticv2
|
||||
def test_annotated_optional_types(clear_sqlmodel) -> None:
|
||||
from pydantic import UUID4
|
||||
|
||||
class Hero(SQLModel, table=True):
|
||||
# Pydantic UUID4 is: Annotated[UUID, UuidVersion(4)]
|
||||
id: Optional[UUID4] = Field(default_factory=uuid.uuid4, primary_key=True)
|
||||
|
||||
engine = create_engine("sqlite:///:memory:")
|
||||
SQLModel.metadata.create_all(engine)
|
||||
with Session(engine) as db:
|
||||
hero = Hero()
|
||||
db.add(hero)
|
||||
db.commit()
|
||||
statement = select(Hero)
|
||||
result = db.exec(statement).all()
|
||||
assert len(result) == 1
|
||||
assert isinstance(hero.id, uuid.UUID)
|
||||
Reference in New Issue
Block a user