tortoise / tortoise/tortoise-orm

Allow auto_now and auto_now_add to use server side now() function

Open
#991 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Is your feature request related to a problem? Please describe.
When implementing created_at and updated_at columns with tortoise-orm, created_at and updated_at are set to the time of the client - which is not necessarily desired. Additionally if a record is created with both updated_at and created_at columns, they are different, as the timestamp value is resolved at different times (very slightly differently, but, different all the same).

I note that CURRENT_TIMESTAMP is the column default, but there doesn't seem to be any cases (which I can trigger anyway) where that is used - we seem to always set the value in INSERT or UPDATE queries.

Describe the solution you'd like
An option on DatetimeField, which uses the server-side now() function (i.e. the transaction timestamp), or DEFAULT to use CURRENT_TIMESTAMP.

Describe alternatives you've considered
I've considered implementing this in signals or something - which I may do until tortoise-api has this functionality (once I can figure out how to set a value to a function's result). Ultimately though, I believe this functionality should live in Tortoise.

Additional context
None I can think of a the moment.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at DatetimeField and trace how auto_now and auto_now_add values are added to INSERT and UPDATE queries. Compare the requested server-side now() and DEFAULT behavior; done means an option uses server-side timestamps and keeps created_at and updated_at consistent when both are set.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.