Skip to content

fix(persistence): honour the utc flag when detecting changed date columns - #12949

Merged
alumni merged 3 commits into
typeorm:masterfrom
maxymlyskov:fix/honour-utc-flag-in-date-change-detection
Oct 11, 2026
Merged

alumni merged 3 commits into
typeorm:masterfrom
maxymlyskov:fix/honour-utc-flag-in-date-change-detection

Conversation

@maxymlyskov

@maxymlyskov maxymlyskov commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Description of change

Fixes #12945

With @Column({ type: "date", utc: true }), save() drops an edit to the column when the process runs west of UTC. Under TZ=America/New_York, load a row holding 2025-05-31, set the column to new Date(Date.UTC(2025, 5, 1)) and save(): no UPDATE goes out and the row keeps 2025-05-31. update() with the same Date writes 2025-06-01.

SubjectChangedColumnsComputer formats the new value with DateUtils.mixedDateToDateString(normalizedValue) and no utc, so in New York 2025-06-01T00:00Z comes out as 2025-05-31, equals the stored value, and the column is skipped:

case "date":
normalizedValue = column.isArray
? normalizedValue.map((date: Date) =>
DateUtils.mixedDateToDateString(date),
)
: DateUtils.mixedDateToDateString(
normalizedValue,
)
databaseValue = column.isArray
? databaseValue.map((date: Date) =>
DateUtils.mixedDateToDateString(date),
)
: DateUtils.mixedDateToDateString(databaseValue)

#11740 added the option and passes { utc: columnMetadata.utc } on every driver's write and hydrate path. Change detection was the one place it missed, so now both sides of that comparison, the new value and the stored one, get { utc: column.utc }. utc: false columns format the same as before. I left date[] alone, since its write path ignores utc as well.

Probe on master vs this branch, better-sqlite3 and postgres 17, New York (UTC and Tokyo were already fine):

master  sqlite TZ=America/New_York save() sent 0 UPDATE -> db 2025-05-31; control update() -> db 2025-06-01
master  pg     TZ=America/New_York save() sent 0 UPDATE -> db 2025-05-31; control update() -> db 2025-06-01
branch  sqlite TZ=America/New_York save() sent 1 UPDATE -> db 2025-06-01; control update() -> db 2025-06-01
branch  pg     TZ=America/New_York save() sent 1 UPDATE -> db 2025-06-01; control update() -> db 2025-06-01

The test extends date-utc.test.ts, which already runs in New York. It moves utcDate to the next UTC day and localDate to 22:00 the day before (local), saves, and reads both back. The localDate half catches a fix that formats everything in UTC. On master it fails on better-sqlite3, sqljs and postgres with expected '2025-05-31' to equal '2025-06-01', and passes with the fix.

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links a relevant issue using a closing keyword:
    Fixes #NNNN, Closes #NNNN, or Resolves #NNNN
  • There are new or updated tests validating the change (tests/**.test.ts)
  • Documentation has been updated to reflect this change (docs/docs/**.md): N/A

…umns

The change detector formatted the new value of a date column with the
local calendar day, while every driver writes a utc: true column with
the UTC day. West of UTC, a UTC-midnight Date one day after the stored
value formatted as the stored day, so save() sent no UPDATE.

Fixes typeorm#12945
@github-actions github-actions Bot added the linked-issue PR references an issue label Oct 8, 2026
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Unchanged transformed dates trigger updates ✓ Resolved
Description
SubjectChangedColumnsComputer now formats the entity-side date in UTC, but still formats the
database-side date in local time after applying its transformer. When a UTC date column’s
transformer returns a Date from its stored value, an unchanged June 1 date appears to differ under
TZ=America/New_York, so save() issues an unnecessary update.
Code

packages/typeorm/src/persistence/SubjectChangedColumnsComputer.ts[117]

+                                      { utc: column.utc },
Evidence
The detector applies the transformer when reading the database entity, then formats only the
entity-side scalar date with column.utc. PostgreSQL hydrates a date before applying
transformer.from, so a transformer such as from: value => new Date(value), to: value => value
yields a database-side Date; DateUtils formats that UTC-midnight value as May 31 locally but
June 1 with the new UTC option.

packages/typeorm/src/persistence/SubjectChangedColumnsComputer.ts[93-125]
packages/typeorm/src/metadata/ColumnMetadata.ts[863-866]
packages/typeorm/src/driver/postgres/PostgresDriver.ts[886-896]
packages/typeorm/src/driver/postgres/PostgresDriver.ts[997-1003]
packages/typeorm/src/util/DateUtils.ts[29-57]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
An unchanged `utc: true` date can be marked as changed when a transformer returns a `Date`: the entity-side value is formatted in UTC, while the database-side value is formatted in local time.
## Fix Focus Areas
- packages/typeorm/src/persistence/SubjectChangedColumnsComputer.ts[107-125]
- packages/typeorm/test/functional/columns/date-utc/date-utc.test.ts[50-76]
## Recommended Fix
Apply the column’s UTC option when formatting the database-side scalar date as well. Add a regression test with a UTC date column whose transformer returns a `Date`, verifying that saving an unchanged loaded entity does not issue an update in New York.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Grey Divider

Tip of the day
💡 Did you know, you can tweak Display settings with a live preview to see your comment before it ships

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

The previous commit formatted only the new value of a utc: true date
column in UTC. The stored value still used the local day, so a column
whose transformer returns a Date compared 2025-06-01 against 2025-05-31
in New York and save() sent an UPDATE for an unchanged entity. Both
sides now use { utc: column.utc }.

The new test loads such a row, saves it unchanged and checks that the
version column stays at 1. It passes on master, fails on the previous
commit and passes here. Found by the Qodo review on this PR.
@pkg-pr-new

pkg-pr-new Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm@12949
    

commit: 9172048

@alumni
alumni merged commit fe92394 into typeorm:master Oct 11, 2026
46 checks passed
@github-actions github-actions Bot added this to the 2.0.next milestone Oct 11, 2026
hktitof added a commit to hktitof/typeorm that referenced this pull request Nov 25, 2026
A date array reached the driver as Date objects, because
mixedDateToDateString() hands an array back unchanged, so pg serialized
each element in the process time zone and the column kept the local
calendar day. With utc: true a day west of UTC was stored one day
early, and saving the entity again wrote the same wrong day back.

Format the elements of an array column the way the scalar path already
does, in both drivers that support array columns. This is the array arm
of typeorm#12949, which fixed the changed-column comparison.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

save() does not update a utc: true date column when the process runs west of UTC

3 participants