onupdate
functionality not available?
#990
Unanswered
Julian-J-S
asked this question in
Questions
Replies: 3 comments
-
You can use class UpdatedAt(SQLModel):
updated_at: Optional[datetime] = Field(
default=None,
sa_type=sa.DateTime(timezone=True),
sa_column_kwargs={
'onupdate': sa.func.now(),
'server_default': sa.func.now(),
},
) |
Beta Was this translation helpful? Give feedback.
0 replies
-
Would be nice to have native support in created_at: datetime = Field(sa_column=Column(DateTime, default=func.now()))
updated_at: datetime = Field(sa_column=Column(DateTime, default=func.now(), onupdate=func.now())) |
Beta Was this translation helpful? Give feedback.
0 replies
-
I set the updater id and update time in the add function👇🏻 from datetime import datetime
from sqlmodel import Session as _Session
class Session(_Session):
def add(self, instance: BaseTable, *args, **kwargs) -> None:
user_id = self.info['user_id']
if isinstance(instance, CreatorUpdaterMixin) and user_id:
if not instance.creator:
instance.created_by = user_id
instance.create_at = datetime.now()
else:
instance.updated_by = user_id
instance.update_at = datetime.now() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First Check
Commit to Help
Example Code
Description
I would like my models / tables to have a
created_at
andupdated_at
field that automatically get set on creation or update.When working with FastAPI / sqlalchemy I have multple options to specify that using
server_default
: which creates the timestamp on the dbAs far as I have seen in the docs there is only a
default
: probably also called by the db itselfbut there is no
onupdate
or similar?I tried to integrete the sqlalchemy fields into SQLModel but this did not work.
Any way to achieve this or is this something thats in development?
thanks!
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.19
Python Version
3.10
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions