Skip to content

fix: default created_at and updated_at to now() so a non-UTC session does not shift them - #89

Merged
AlexeyShalaev merged 1 commit into
masterfrom
fix/datetime-defaults-session-time-zone
Nov 12, 2026
Merged

AlexeyShalaev merged 1 commit into
masterfrom
fix/datetime-defaults-session-time-zone

Conversation

@AlexeyShalaev

@AlexeyShalaev AlexeyShalaev commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Closes #88.

DatetimeColumnsMixin now uses now() for the created_at and updated_at server defaults and for the updated_at onupdate. timezone('UTC', now()) is a timestamp without a zone, and the cast into the timestamptz column reads it in the session TimeZone, so in a non-UTC session every value was off by that zone's offset.

The new integration test sets SET LOCAL TIME ZONE 'Europe/Moscow' and checks that both columns equal now() of the writing transaction, once on insert and once on update. On master both cases fail by exactly 3 hours (18:38:04 stored, 21:38:04 expected). The unit tests now check that the defaults render as now().

The upgrade needs a hand-written migration, and the issue's last paragraph got that wrong. The model change fixes onupdate at once, because SQLAlchemy sends it with each UPDATE. A table created on 0.5.1 or earlier keeps DEFAULT timezone('UTC'::text, now()) in the database, though. Autogenerate does not pick that up even with compare_server_default=True. On PostgreSQL, Alembic compares two defaults by running SELECT <db default> = <model default> on the server, and in a UTC session the two are equal. I checked this on a table created with the 0.5.1 DDL: compare_metadata returned [] with the flag off and with it on. After op.alter_column(..., server_default=sa.text("now()")) an insert from a Moscow session was exact.

The quick start now explains the default and gives that migration. The agents page has the new defaults in the Base ORM table and a row under "Fixed since 0.2.0" with the workaround for older versions: set the session time zone to UTC.

Checks: ruff format, ruff check, mypy clean. The full suite has 667 passed, coverage 94.24%. The docs build reports no issues.

BEGIN_COMMIT_OVERRIDE
fix: default created_at and updated_at to now() so a non-UTC session does not shift them (#89)
END_COMMIT_OVERRIDE

…does not shift them

timezone('UTC', now()) is a timestamp without zone, and the cast into the timestamptz column reads it in the session TimeZone. In a Europe/Moscow session both columns ended up 3 hours in the past.

Closes #88
@codecov

codecov Bot commented Nov 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@AlexeyShalaev
AlexeyShalaev merged commit 5b1493d into master Nov 12, 2026
9 checks passed
@AlexeyShalaev
AlexeyShalaev deleted the fix/datetime-defaults-session-time-zone branch November 12, 2026 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DatetimeColumnsMixin defaults are shifted by the session time zone: timezone('UTC', now()) into a timestamptz column

1 participant