Skip to content

Conversation

@RobertRosca
Copy link
Contributor

Issue #370 is a common usecase and there have been a few other questions about how to implement created_at or updated_at timestamps in rows. This PR adds examples to the advanced section of the documentation on how this can be done with sa_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.

@RobertRosca RobertRosca force-pushed the docs/created_updated_at_cols branch from d53330d to 893bcf3 Compare September 3, 2022 09:12
@codecov

This comment was marked as outdated.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 3, 2022

📝 Docs preview for commit 893bcf3 at: https://63131ab0d8aeff704c33fd2d--sqlmodel.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 1e5caaa at: https://6326cad9fcbd027752da81e2--sqlmodel.netlify.app

@github-actions
Copy link
Contributor

github-actions bot commented Nov 2, 2022

📝 Docs preview for commit d78fc71 at: https://636244bbeb67cb7064300959--sqlmodel.netlify.app

@RobertRosca
Copy link
Contributor Author

@github-actions
Copy link
Contributor

📝 Docs preview for commit b160fc8 at: https://639ce08a1f184e006c3a2967--sqlmodel.netlify.app

@tiangolo tiangolo added the docs Improvements or additions to documentation label Oct 22, 2023
@MRigal
Copy link

MRigal commented Feb 16, 2024

This is actually very valuable documentation which I was a bit missing. What is it missing to get merged?

@github-actions

This comment was marked as outdated.

Copy link
Member

@YuriiMotov YuriiMotov left a 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?

Comment on lines +57 to +70
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()
```
Copy link
Member

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())
Copy link
Member

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())
Copy link
Member

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?

Comment on lines +36 to +37
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
Copy link
Member

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Member

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

@YuriiMotov YuriiMotov changed the title 📝 Add example code for sa_column onupdate timestamps 📝 Add code example for sa_column onupdate timestamps Aug 25, 2025
@github-actions
Copy link
Contributor

📝 Docs preview for commit bf882be at: https://dfa3bd1d.sqlmodel.pages.dev

Modified Pages

@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot closed this Sep 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation investigate waiting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants