SQLModel Fails to Convert ISO 8601 Datetime Strings to Python datetime Objects #1253
-
First Check
Commit to Help
Example Codefrom sqlmodel import SQLModel, Field, Relationship
from datetime import datetime, timezone
from typing import Optional, List
from sqlalchemy import JSON
from sqlalchemy.sql import func
import base64, uuid
import sqlalchemy
class Guild(SQLModel, table=True):
__tablename__ = "guilds"
id: int = Field(sa_column=sqlalchemy.Column(sqlalchemy.BigInteger , primary_key=True , index=True)
)
client_id: int = Field(
sa_column=sqlalchemy.Column(sqlalchemy.BigInteger, index=True , primary_key=True)
)
blacklisted: bool = Field(default=False)
prefix: Optional[str] = Field(default=None, nullable=True)
embed_color: Optional[str] = Field(default=None, nullable=True)
volume: int = Field(default=75)
autoplay: bool = Field(default=False)
buttons: bool = Field(default=True)
announce: bool = Field(default=True)
announce_channel: Optional[int] = Field(
default=None,
sa_column=sqlalchemy.Column(sqlalchemy.BigInteger, nullable=True),
)
leavecleanup: bool = Field(default=False)
leaveempty: bool = Field(default=False)
voicestatus: bool = Field(default=True)
dj_commands: List[str] = Field(default_factory=list, sa_type=JSON)
dj_role: Optional[int] = Field(
default=None,
sa_column=sqlalchemy.Column(sqlalchemy.BigInteger, nullable=True),
)
dj_temp: bool = Field(default=False)
reconnect: bool = Field(default=False)
text_channel: Optional[int] = Field(
default=None,
sa_column=sqlalchemy.Column(sqlalchemy.BigInteger, nullable=True),
)
voice_channel: Optional[int] = Field(
default=None,
sa_column=sqlalchemy.Column(sqlalchemy.BigInteger, nullable=True),
)
reconnect_vc: Optional[int] = Field(
default=None,
sa_column=sqlalchemy.Column(sqlalchemy.BigInteger, nullable=True),
)
request_channel: Optional[int] = Field(
default=None,
sa_column=sqlalchemy.Column(sqlalchemy.BigInteger, nullable=True),
)
request_message: Optional[int] = Field(
default=None,
sa_column=sqlalchemy.Column(sqlalchemy.BigInteger, nullable=True),
)
allowed_channels: List[int] = Field(default_factory=list, sa_type=JSON)
blocked_channels: List[int] = Field(default_factory=list, sa_type=JSON)
user_queue_limit: Optional[int] = Field(default=None, nullable=True)
created_at: datetime = Field(
default_factory=lambda: datetime.now(timezone.utc),
sa_column=sqlalchemy.Column(
sqlalchemy.DateTime(timezone=True), server_default=sqlalchemy.func.now()
),
)
updated_at: datetime = Field(
default_factory=lambda: datetime.now(timezone.utc),
sa_column=sqlalchemy.Column(
sqlalchemy.DateTime(timezone=True),
server_default=sqlalchemy.func.now(),
onupdate=func.now(),
),
)
class Config:
arbitrary_types_allowed = True Description
guild = Guild(id=1, client_id=1)
print(f"Initial Type of Updated At: {type(guild.updated_at)}")
guild_dumped = guild.model_dump_json()
guild_reconstructed = Guild.model_validate_json(guild_dumped)
print(f"Final Type of Updated At: {type(guild_reconstructed.updated_at)}")
print(Guild.__annotations__) Output: Initial Type of Updated at : <class 'datetime.datetime'>
Final Type of Updated At : <class 'str'>
{'id': <class 'int'>, 'client_id': <class 'int'>, 'blacklisted': <class 'bool'>, 'prefix': typing.Optional[str], 'embed_color': typing.Optional[str], 'volume': <class 'int'>, 'autoplay': <class 'bool'>, 'buttons': <class 'bool'>, 'announce': <class 'bool'>, 'announce_channel': typing.Optional[int], 'leavecleanup': <class 'bool'>, 'leaveempty': <class 'bool'>, 'voicestatus': <class 'bool'>, 'dj_commands':
typing.List[str], 'dj_role': typing.Optional[int], 'dj_temp': <class 'bool'>, 'reconnect': <class 'bool'>, 'text_channel': typing.Optional[int], 'voice_channel': typing.Optional[int], 'reconnect_vc': typing.Optional[int], 'request_channel': typing.Optional[int], 'request_message': typing.Optional[int], 'allowed_channels': typing.List[int], 'blocked_channels': typing.List[int], 'user_queue_limit': typing.Optional[int], 'created_at': <class 'datetime.datetime'>, 'updated_at': <class 'datetime.datetime'>} Operating SystemWindows Operating System DetailsEdition Windows 10 Pro
Version 20H2
Installed on 3/4/2024
OS build 19042.685
Experience Windows Feature Experience Pack 120.2212.551.0 SQLModel Version0.0.22 Python VersionPython 3.11.8 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
monchin
Dec 23, 2024
Replies: 3 comments
-
Pydantic version pydantic-core==2.27.2
pydantic==2.10.4 |
Beta Was this translation helpful? Give feedback.
0 replies
-
If i don't pass from sqlmodel import SQLModel , Field
from datetime import datetime , timezone
from typing import Optional
class Test(SQLModel):
id : int
updated_at : Optional[datetime] = Field(default_factory=lambda : datetime.now(timezone.utc))
t1 = Test(id=1)
print(f"Type of updated_at at the starting is : {type(t1.updated_at)}")
t1d = t1.model_dump_json()
print(t1d)
t2re = Test.model_validate_json(t1d)
print(f"Type of updated_at at the end is : {type(t2re.updated_at)}") Output: Type of updated_at at the starting is : <class 'datetime.datetime'>
{"id":1,"updated_at":"2024-12-22T08:06:42.264998Z"}
Type of updated_at at the end is : <class 'datetime.datetime'> |
Beta Was this translation helpful? Give feedback.
0 replies
-
When you are using |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
munishkhatri720
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When you are using
table=True
, don't regard it as pydantic models.See #1041 #52 #811 etc.
Also see the way to handle this issue by the author