Issue with created_at timestamp field not being correctly created in schema with default_factory. How do i add timestamp as a column in table in SQLmodel ? #1295
-
First Check
Commit to Help
Example Codeclass Job(SQLModel, table=True):
id: str = Field(default_factory=lambda: str(uuid.uuid4()), primary_key=True)
user_id: str
created_at: datetime = Field(default_factory=datetime.utcnow) DescriptionProblemWhen defining a model with a Steps to Reproduce
Expected BehaviorThe Environment
Additional InformationIt seems like the field is not automatically treated as a timestamp column in the database when using Possible FixOne solution could be to add support for automatic timestamp handling at the database level or update the documentation for users to handle this behavior manually. Operating SystemWindows Operating System DetailsNo response SQLModel Version0.0.22 Python Version3.10.16 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Can't reproduce the issue (see details) import uuid
from datetime import datetime
from sqlmodel import Field, SQLModel, create_engine
class Job(SQLModel, table=True):
id: str = Field(default_factory=lambda: str(uuid.uuid4()), primary_key=True)
user_id: str
created_at: datetime = Field(default_factory=datetime.utcnow)
engine = create_engine(
"postgresql://user:mysecretpassword@localhost/some_db", echo=True
)
SQLModel.metadata.drop_all(engine)
SQLModel.metadata.create_all(engine)
To specify server-side logic, you should specify import uuid
from datetime import datetime
from sqlmodel import Field, SQLModel, create_engine, func
class Job(SQLModel, table=True):
id: str = Field(default_factory=lambda: str(uuid.uuid4()), primary_key=True)
user_id: str
created_at: datetime = Field(
sa_column_kwargs={
'server_default': func.now(),
},
)
engine = create_engine(
"postgresql://user:mysecretpassword@localhost/some_db", echo=True
)
SQLModel.metadata.drop_all(engine)
SQLModel.metadata.create_all(engine)
|
Beta Was this translation helpful? Give feedback.
Can't reproduce the issue (see details)
To specify server-sid…