You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar question and didn't find it.
I searched the SQLModel documentation, with the integrated search.
I already searched in Google "How to X in SQLModel" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to SQLModel but to Pydantic.
I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
I commit to help with one of those options 👆
Example Code
importdatetimefromsqlmodelimportField, SQLModel, create_engine# OK: table=False, date field is convertedclassTestTableFalse(SQLModel, table=False):
id: int|None=Field(default=None, primary_key=True)
creation_date: datetime.datetype(TestTableFalse(creation_date="2024-01-01").creation_date)
# datetime.date# WRONG: table=True, date field is not convertedclassTestTableTrue(SQLModel, table=True):
id: int|None=Field(default=None, primary_key=True)
creation_date: datetime.datetype(TestTableTrue(creation_date="2024-01-01").creation_date)
# str# pydantic: date field is convertedfrompydanticimportBaseModel, FieldclassTestPydantic(BaseModel):
id: int|None=Field(default=None, primary_key=True)
creation_date: datetime.datetype(TestPydantic(creation_date="2024-01-01").creation_date)
# datetime.date
Description
When SQLModel model is created with table=False fields which are annotated with datetime.date / datetime.datetime are automatically converted to the annotated types. This behavior is consistent with behavior of classes extending pydantic.BaseModel.
However, when table=True, that's no longer the case.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
When
SQLModel
model is created withtable=False
fields which are annotated withdatetime.date
/datetime.datetime
are automatically converted to the annotated types. This behavior is consistent with behavior of classes extendingpydantic.BaseModel
.However, when
table=True
, that's no longer the case.Is this an expected behavior?
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.22
Python Version
3.13.0
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions