Uh oh!
There was an error while loading. Please reload this page.
📝 Add code example for sa_columnonupdate timestamps - #372
Conversation
d53330d to
893bcf3Compare
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 |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
📝 Docs preview for commit d78fc71 at: https://636244bbeb67cb7064300959--sqlmodel.netlify.app |
RobertRosca
commented
Nov 2, 2022
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 |
MRigal
commented
Feb 16, 2024
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.
YuriiMotov
left a comment
There was a problem hiding this comment.
@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.
I would remove this code example with validator
Uh oh!
There was an error while loading. Please reload this page.
| age: Optional[int] = None | ||
| registered_at: datetime = Field( | ||
| sa_column=Column(DateTime(timezone=False), server_default=func.now()) |
| ) | ||
| updated_at: Optional[datetime] = Field( | ||
| sa_column=Column(DateTime(timezone=False), onupdate=func.now()) |
There was a problem hiding this comment.
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.
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.
| defupdate_hero_age(new_secret_name): | |
| defupdate_hero_secret_name(new_secret_name): |
| def main(): | ||
| create_db_and_tables() | ||
| create_heroes() | ||
| sleep(1) |
There was a problem hiding this comment.
We need to find way to avoid sleeping as it will make test run slower
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
sa_columnonupdate 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.