-
-
Notifications
You must be signed in to change notification settings - Fork 607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to add current date time by default on a table declaration? #594
Comments
We've been doing it like this
|
I do the same as @antont but without the from sqlmodel import SQLModel, Field
import datetime
class Article(SQLModel, table=True):
created_at: datetime.datetime = Field(
default_factory=datetime.datetime.utcnow,
) |
lol I don't know why we had the useless lambda, i guess some mindless copy-paste, thanks, will get to clean those up. |
This is the cleanest way I have found to do it core_models.py
consuming.py
I think having opinionated timestamping might not work. Some people might regard update_at as when a new version of the record was created rather than when the record is updated later on |
This is how I use from sqlmodel import SQLModel, Column, Field
from sqlalchemy import DateTime, func
from typing import Optional
import datetime
class Article(SQLModel, table=True):
created_at: datetime.datetime = Field(
default_factory=datetime.datetime.utcnow,
)
updated_at: Optional[datetime.datetime] = Field(
sa_column=Column(DateTime(), onupdate=func.now())
)
Personally I believe this does not make much sense.
|
Just going to throw this out there - created at / updated at / soft deleted at are really common patterns and there might be a bit of value to just make them first class features |
How would you do it? Maybe a mixin in some library module, that one can use to add features to base SQLModel? |
Yes, I think a mixin might work. I do think it's worth having a discussion how created at / updated at / deleted at would fit with created by / updated by / deleted by (so user IDs, which may be an integer or string type) as well as versioned entities (does created / updated just get replaced with "updated", and "created" is implicit with version 1?) |
It's common to delegate generating timestamps to the database rather than generating them in Python. Much like @FilipeMarch is showing in the In that case you would set the server_default attribute on the column. In the case of sqlalchemy it would look like this: I agree it's worth having a firstclass feature for this. |
Is the solution in #594 (comment) the answer? Can the issue be closed? |
I agree generating timestamp should be dealt with by the database. In this case, I currently use this
The advantage of this is I don’t need to refer to an actual sa_column, and using CURRENT_TIMESTAMP adheres to ansi sql standards |
wow, amazing, this is works pretty good with alembic's --autogenerate
|
This can be closed. |
Privileged issue
Issue Content
how to add a field in the table with the current date by default?
The text was updated successfully, but these errors were encountered: