fix(models): use consistent naive_utc_now for created_at/updated_at, not DB server time#38787
Open
iruzen-dono wants to merge 1 commit into
Open
fix(models): use consistent naive_utc_now for created_at/updated_at, not DB server time#38787iruzen-dono wants to merge 1 commit into
iruzen-dono wants to merge 1 commit into
Conversation
…not DB server time On MySQL, `func.current_timestamp()` returns the database server's local time (e.g., CST/UTC+8), while the service code updates `updated_at` with `naive_utc_now()` (Python UTC). Since MySQL's DATETIME type does not store timezone, this creates an 8-hour discrepancy between created_at and updated_at. Fix by switching Conversation and Message from DB-side `server_default=func.current_timestamp()` / `onupdate=func.current_timestamp()` to Python-side `default=naive_utc_now` / `onupdate=naive_utc_now`. This ensures both fields always get UTC values regardless of the database server timezone. Fixes langgenius#38553
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Bug
On MySQL,
func.current_timestamp()returns the database server's local time (e.g., CST = UTC+8), while the service code updatesupdated_atwithnaive_utc_now()(Python UTC). Since MySQL's DATETIME type does not store timezone info,created_atandupdated_atend up with different timezone offsets — typically an 8-hour discrepancy for users in CST.The Fix
Switch Conversation and Message models from DB-side
server_default=func.current_timestamp()/onupdate=func.current_timestamp()to Python-sidedefault=naive_utc_now/onupdate=naive_utc_now.This ensures both
created_atandupdated_atalways get UTC values, regardless of the database server's timezone.Changes
api/models/model.py: importnaive_utc_nowfromlibs.datetime_utilsand replacefunc.current_timestamp()references withnaive_utc_nowAffected columns
server_default=func.current_timestamp()default=naive_utc_nowserver_default=func.current_timestamp(), onupdate=func.current_timestamp()default=naive_utc_now, onupdate=naive_utc_nowserver_default=func.current_timestamp()default=naive_utc_nowserver_default=func.current_timestamp(), onupdate=func.current_timestamp()default=naive_utc_now, onupdate=naive_utc_nowReferences
Closes #38553