fix(db): 使用 lambda 表达式确保 updated_at 字段正确更新 #4730
Open
+19
−61
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.
Modifications / 改动点
updated_at 字段原先在 sa_column_kwargs["onupdate"] 中直接使用了 datetime.now(),导致时间戳仅在模块导入时被计算一次,之后不再变化,结果所有记录的更新时间都被设成了程序启动时间。
本次修改将时间戳生成逻辑封装进 lambda 表达式,使 SQLAlchemy 在每次更新记录时才惰性求值,从而保证时间戳实时更新。
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
requirements.txt和pyproject.toml文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations inrequirements.txtandpyproject.toml.Summary by Sourcery
错误修复:
updated_at字段,使其时间戳在更新时评估,而不是仅在导入时评估一次。Original summary in English
Summary by Sourcery
Bug Fixes:
updated_atfields across multiple SQLModel entities so their timestamps are evaluated at update time instead of only once at import.