-
-
Notifications
You must be signed in to change notification settings - Fork 782
📝 Add code example for sa_column onupdate timestamps
#372
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
📝 Add code example for sa_column onupdate timestamps
#372
Conversation
d53330d to
893bcf3
Compare
This comment was marked as outdated.
This comment was marked as outdated.
|
📝 Docs preview for commit 893bcf3 at: https://63131ab0d8aeff704c33fd2d--sqlmodel.netlify.app |
|
📝 Docs preview for commit 1e5caaa at: https://6326cad9fcbd027752da81e2--sqlmodel.netlify.app |
|
📝 Docs preview for commit d78fc71 at: https://636244bbeb67cb7064300959--sqlmodel.netlify.app |
|
Full path to the docs page: https://636244bbeb67cb7064300959--sqlmodel.netlify.app/advanced/sa-column/ |
|
📝 Docs preview for commit b160fc8 at: https://639ce08a1f184e006c3a2967--sqlmodel.netlify.app |
|
This is actually very valuable documentation which I was a bit missing. What is it missing to get merged? |
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RobertRosca, thanks for working on this!
I think it would be better to start from describing the Pydantic implementation (only default_factory, no need to focus on this that much), then highlight its weak points and then present the solution with server_default and onupdate.
Also, code example should be covered by test.
Later we could also add an alternative way to implement this using sa_type and sa_column_kwargs.
See also my other in-code comments.
Are you ready to continue working on this?
| Another approach is to use a Pydantic `validator`: | ||
|
|
||
| ```python | ||
| from datetime import datetime | ||
|
|
||
| from pydantic import BaseModel, validator | ||
|
|
||
| class Model(BaseModel): | ||
| created_at: datetime = None | ||
|
|
||
| @validator('ts', pre=True, always=True) | ||
| def set_created_at_now(cls, v): | ||
| return v or datetime.now() | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this code example with validator
| age: Optional[int] = None | ||
|
|
||
| registered_at: datetime = Field( | ||
| sa_column=Column(DateTime(timezone=False), server_default=func.now()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add nullable=False?
| ) | ||
|
|
||
| updated_at: Optional[datetime] = Field( | ||
| sa_column=Column(DateTime(timezone=False), onupdate=func.now()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add server_default=func.now() and nullable=False?
| hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") | ||
| hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only update hero1, I would remove hero2 and hero3 to simplify the example
| session.close() | ||
|
|
||
|
|
||
| def update_hero_age(new_secret_name): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def update_hero_age(new_secret_name): | |
| def update_hero_secret_name(new_secret_name): |
| def main(): | ||
| create_db_and_tables() | ||
| create_heroes() | ||
| sleep(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to find way to avoid sleeping as it will make test run slower
sa_column onupdate timestamps
|
📝 Docs preview for commit bf882be at: https://dfa3bd1d.sqlmodel.pages.dev Modified Pages |
|
As this PR has been waiting for the original user for a while but seems to be inactive, it's now going to be closed. But if there's anyone interested, feel free to create a new PR. |
Issue #370 is a common usecase and there have been a few other questions about how to implement
created_atorupdated_attimestamps in rows. This PR adds examples to the advanced section of the documentation on how this can be done withsa_columns, and also links the relevant sections of Pydantic documentation which show how to use factories or validators to achieve a similar thing with Pydantic.