SQLModel cannot fetch the type of a enum field containing Annotated type #903
-
First Check
Commit to Help
Example Codedef iso_format(dt: datetime) -> str:
return dt.isoformat()
ISOTimestamp = Annotated[
datetime,
PlainSerializer(iso_format, return_type=str),
]
class Product(BaseModel, table=True):
created: ISOTimestamp = Field(default_factory=utc_now, nullable=False)
deprecated: ISOTimestamp | None = Field(default=None) # Works if I remove this field DescriptionWhen loading the above model I get
Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.16 Python Version3.11.7 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
joachimhuet
Apr 26, 2024
Replies: 1 comment 1 reply
-
In fact this error comes from sqlalchemy, you can see that the error disapeear if you remove the So what you can do is specify a Something like that should fix your problem: from sqlalchemy import DateTime # if you want the DateTime type or String if you want it to be a string
class Product(BaseModel, table=True):
created: ISOTimestamp = Field(default_factory=utc_now, nullable=False, sa_type=DateTime)
deprecated: ISOTimestamp | None = Field(default=None, sa_type=DateTime) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
fubuloubu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In fact this error comes from sqlalchemy, you can see that the error disapeear if you remove the
tablle=True
So what you can do is specify a
sa_type
to make sqlalchemy understand what type you actually want to be used in the database side.Something like that should fix your problem: