Skip to content

fix: skip precision/scale comparison for spatial columns in postgres schema sync - #12801

Open
mkovac-conductive wants to merge 1 commit into
typeorm:masterfrom
mkovac-conductive:fix/postgres-spatial-precision-phantom-diff
Open

mkovac-conductive wants to merge 1 commit into
typeorm:masterfrom
mkovac-conductive:fix/postgres-spatial-precision-phantom-diff

Conversation

@mkovac-conductive

@mkovac-conductive mkovac-conductive commented Aug 26, 2026 •

Copy link
Copy Markdown

Geometry/geography columns have no precision or scale in the Postgres catalog, so the value set in entity metadata (used only to control the ST_AsGeoJSON precision on read) never matches what is introspected back. This caused migration:generate to emit a no-op

ALTER TABLE ... ALTER COLUMN ... TYPE geometry(...)

on every run, an endless phantom diff that could never be resolved.

Skip the precision/scale comparison for spatial column types so it only runs where precision/scale actually round-trip through the database.

Closes #12800

Description of change

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)

🛡️ Security fixes: Do not submit security fixes as public PRs — the diff exposes the vulnerability. Use GitHub Security Advisories instead.

@github-actions github-actions Bot added the linked-issue PR references an issue label Aug 26, 2026
@mkovac-conductive mkovac-conductive changed the title fix: skip precision/scale comparison for spatial columns in postgres … fix: skip precision/scale comparison for spatial columns in postgres schema sync Aug 26, 2026
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (1) 📜 Skill insights (0)

Grey Divider


Action required

1. Aliases skip precision changes 🐞 Bug ≡ Correctness ⭐ New
Description
The new precision guards test raw type names, so supported aliases such as timestamp,
timestamptz, time, and timetz are absent from withPrecisionColumnTypes and precision-only
changes are silently ignored. This breaks both metadata-driven schema synchronization and direct
changeColumn calls for those aliases.
Code

src/driver/postgres/PostgresDriver.ts[R1491-1493]

+                (this.withPrecisionColumnTypes.indexOf(columnMetadata.type) !==
+                    -1 &&
+                    tableColumn.precision !== columnMetadata.precision) ||
Evidence
The driver explicitly supports the aliases, normalizes them to canonical precision-bearing types,
but its precision capability list contains only those canonical names. The new Driver guard examines
unnormalized metadata, and the sibling QueryRunner guard likewise examines the raw TableColumn type;
repository usage confirms timestamp with precision is a real supported declaration.

src/driver/postgres/PostgresDriver.ts[156-191]
src/driver/postgres/PostgresDriver.ts[262-270]
src/driver/postgres/PostgresDriver.ts[1164-1171]
src/driver/postgres/PostgresDriver.ts[1486-1497]
src/driver/postgres/PostgresQueryRunner.ts[1621-1644]
test/github-issues/8747/entity/Car.ts[12-18]

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

## Issue description
Precision-only changes are skipped for PostgreSQL temporal aliases because the new capability checks compare raw alias names against a canonical-type list.

## Issue Context
PostgresDriver.normalizeType maps `timestamp`, `timestamptz`, `time`, and `timetz` to canonical precision-bearing types. Apply equivalent normalization consistently in both changed guards so schema-builder and direct QueryRunner paths retain precision updates while spatial types remain excluded.

## Fix Focus Areas
- src/driver/postgres/PostgresDriver.ts[1491-1497]
- src/driver/postgres/PostgresQueryRunner.ts[1622-1629]

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


2. Issue reference missing 📘 Rule violation ⚙ Maintainability
Description
The regression test for issue #12800 does not include the required issue reference in a test
comment. This weakens traceability between the functional regression coverage and the issue it
fixes.
Code

test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21]

+    it("should not generate migrations for spatial columns with precision participating in a composite index", async () => {
Evidence
PR Compliance ID 3 requires functional issue-fix tests to include an issue reference in a test
comment when applicable. The added test at line 21 describes the behavior but contains no comment
referencing #12800.

Rule 3: Prefer functional tests over per-issue tests
test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21-21]

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

## Issue description
The new functional regression test does not identify GitHub issue `#12800` in a test comment as required for applicable issue fixes.
## Issue Context
The PR closes `#12800`, so the new regression test should retain that traceability with a nearby comment.
## Fix Focus Areas
- test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21-21]

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


3. Spatial constraints still churn 🐞 Bug ≡ Correctness
Description
The new guard suppresses only the ALTER COLUMN after findChangedColumns has already classified
the spatial precision mismatch as a real change, so schema synchronization still drops and recreates
related foreign keys, composite indexes, and composite unique constraints. Consequently, indexed or
constrained spatial columns can keep producing destructive phantom migrations on every run even
though the new test with a bare column passes.
Code

src/driver/postgres/PostgresQueryRunner.ts[R1627-1629]

+                !isSpatialColumn &&
+                (newColumn.precision !== oldColumn.precision ||
+                    newColumn.scale !== oldColumn.scale)
Evidence
Postgres change detection still compares precision and scale unconditionally, and every detected
changed column enters pre-change cleanup that drops related foreign keys, composite indexes, and
composite unique constraints. Those objects are recreated only in later schema-sync phases, proving
that the query-runner-only guard can still generate drop/create SQL for the phantom difference.

src/driver/postgres/PostgresDriver.ts[1486-1493]
src/schema-builder/RdbmsSchemaBuilder.ts[893-927]
src/schema-builder/RdbmsSchemaBuilder.ts[1229-1283]
src/schema-builder/RdbmsSchemaBuilder.ts[1293-1345]
src/schema-builder/RdbmsSchemaBuilder.ts[227-245]
src/driver/postgres/PostgresQueryRunner.ts[1621-1645]

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

## Issue description
Spatial precision/scale mismatches are ignored only inside `PostgresQueryRunner.changeColumn`, after the schema builder has already dropped related foreign keys, composite indexes, and composite unique constraints. Exclude precision/scale from Postgres spatial-column change detection so these columns do not enter the update flow when no real database property differs.
## Issue Context
`geometry` and `geography` precision/scale do not round-trip through PostgreSQL. Keep comparison of all other column properties, including spatial feature type and SRID, and add coverage for a spatial column participating in a related index or constraint.
## Fix Focus Areas
- src/driver/postgres/PostgresDriver.ts[1486-1493]
- src/driver/postgres/PostgresQueryRunner.ts[1621-1629]
- test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21-30]

ⓘ 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 ask Qodo to dismiss a finding you disagree with, with your reason on record

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Previous reviews

Review updated until commit 673232c ⚖️ Balanced

Results up to commit 5a8c652


🐞 Bugs (1) 📘 Rule violations (1) 📜 Skill insights (0)


Action required
1. Issue reference missing 📘 Rule violation ⚙ Maintainability
Description
The regression test for issue #12800 does not include the required issue reference in a test
comment. This weakens traceability between the functional regression coverage and the issue it
fixes.
Code

test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21]

+    it("should not generate migrations for spatial columns with precision participating in a composite index", async () => {
Evidence
PR Compliance ID 3 requires functional issue-fix tests to include an issue reference in a test
comment when applicable. The added test at line 21 describes the behavior but contains no comment
referencing #12800.

Rule 3: Prefer functional tests over per-issue tests
test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21-21]

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

## Issue description
The new functional regression test does not identify GitHub issue `#12800` in a test comment as required for applicable issue fixes.
## Issue Context
The PR closes `#12800`, so the new regression test should retain that traceability with a nearby comment.
## Fix Focus Areas
- test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21-21]

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


2. Spatial constraints still churn 🐞 Bug ≡ Correctness
Description
The new guard suppresses only the ALTER COLUMN after findChangedColumns has already classified
the spatial precision mismatch as a real change, so schema synchronization still drops and recreates
related foreign keys, composite indexes, and composite unique constraints. Consequently, indexed or
constrained spatial columns can keep producing destructive phantom migrations on every run even
though the new test with a bare column passes.
Code

src/driver/postgres/PostgresQueryRunner.ts[R1627-1629]

+                !isSpatialColumn &&
+                (newColumn.precision !== oldColumn.precision ||
+                    newColumn.scale !== oldColumn.scale)
Evidence
Postgres change detection still compares precision and scale unconditionally, and every detected
changed column enters pre-change cleanup that drops related foreign keys, composite indexes, and
composite unique constraints. Those objects are recreated only in later schema-sync phases, proving
that the query-runner-only guard can still generate drop/create SQL for the phantom difference.

src/driver/postgres/PostgresDriver.ts[1486-1493]
src/schema-builder/RdbmsSchemaBuilder.ts[893-927]
src/schema-builder/RdbmsSchemaBuilder.ts[1229-1283]
src/schema-builder/RdbmsSchemaBuilder.ts[1293-1345]
src/schema-builder/RdbmsSchemaBuilder.ts[227-245]
src/driver/postgres/PostgresQueryRunner.ts[1621-1645]

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

## Issue description
Spatial precision/scale mismatches are ignored only inside `PostgresQueryRunner.changeColumn`, after the schema builder has already dropped related foreign keys, composite indexes, and composite unique constraints. Exclude precision/scale from Postgres spatial-column change detection so these columns do not enter the update flow when no real database property differs.
## Issue Context
`geometry` and `geography` precision/scale do not round-trip through PostgreSQL. Keep comparison of all other column properties, including spatial feature type and SRID, and add coverage for a spatial column participating in a related index or constraint.
## Fix Focus Areas
- src/driver/postgres/PostgresDriver.ts[1486-1493]
- src/driver/postgres/PostgresQueryRunner.ts[1621-1629]
- test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21-30]

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


Results up to commit 7bdaa00


🐞 Bugs (1) 📘 Rule violations (1) 📜 Skill insights (0)


Action required
1. Issue reference missing 📘 Rule violation ⚙ Maintainability ⭐ New
Description
The regression test for issue #12800 does not include the required issue reference in a test
comment. This weakens traceability between the functional regression coverage and the issue it
fixes.
Code

test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21]

+    it("should not generate migrations for spatial columns with precision participating in a composite index", async () => {
Evidence
PR Compliance ID 3 requires functional issue-fix tests to include an issue reference in a test
comment when applicable. The added test at line 21 describes the behavior but contains no comment
referencing #12800.

Rule 3: Prefer functional tests over per-issue tests
test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21-21]

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

## Issue description
The new functional regression test does not identify GitHub issue `#12800` in a test comment as required for applicable issue fixes.

## Issue Context
The PR closes `#12800`, so the new regression test should retain that traceability with a nearby comment.

## Fix Focus Areas
- test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21-21]

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


2. Spatial constraints still churn 🐞 Bug ≡ Correctness
Description
The new guard suppresses only the ALTER COLUMN after findChangedColumns has already classified
the spatial precision mismatch as a real change, so schema synchronization still drops and recreates
related foreign keys, composite indexes, and composite unique constraints. Consequently, indexed or
constrained spatial columns can keep producing destructive phantom migrations on every run even
though the new test with a bare column passes.
Code

src/driver/postgres/PostgresQueryRunner.ts[R1627-1629]

+                !isSpatialColumn &&
+                (newColumn.precision !== oldColumn.precision ||
+                    newColumn.scale !== oldColumn.scale)
Evidence
Postgres change detection still compares precision and scale unconditionally, and every detected
changed column enters pre-change cleanup that drops related foreign keys, composite indexes, and
composite unique constraints. Those objects are recreated only in later schema-sync phases, proving
that the query-runner-only guard can still generate drop/create SQL for the phantom difference.

src/driver/postgres/PostgresDriver.ts[1486-1493]
src/schema-builder/RdbmsSchemaBuilder.ts[893-927]
src/schema-builder/RdbmsSchemaBuilder.ts[1229-1283]
src/schema-builder/RdbmsSchemaBuilder.ts[1293-1345]
src/schema-builder/RdbmsSchemaBuilder.ts[227-245]
src/driver/postgres/PostgresQueryRunner.ts[1621-1645]

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

## Issue description
Spatial precision/scale mismatches are ignored only inside `PostgresQueryRunner.changeColumn`, after the schema builder has already dropped related foreign keys, composite indexes, and composite unique constraints. Exclude precision/scale from Postgres spatial-column change detection so these columns do not enter the update flow when no real database property differs.
## Issue Context
`geometry` and `geography` precision/scale do not round-trip through PostgreSQL. Keep comparison of all other column properties, including spatial feature type and SRID, and add coverage for a spatial column participating in a related index or constraint.
## Fix Focus Areas
- src/driver/postgres/PostgresDriver.ts[1486-1493]
- src/driver/postgres/PostgresQueryRunner.ts[1621-1629]
- test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21-30]

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


Results up to commit 0468025


🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)


Action required
1. Spatial constraints still churn 🐞 Bug ≡ Correctness
Description
The new guard suppresses only the ALTER COLUMN after findChangedColumns has already classified
the spatial precision mismatch as a real change, so schema synchronization still drops and recreates
related foreign keys, composite indexes, and composite unique constraints. Consequently, indexed or
constrained spatial columns can keep producing destructive phantom migrations on every run even
though the new test with a bare column passes.
Code

src/driver/postgres/PostgresQueryRunner.ts[R1627-1629]

+                !isSpatialColumn &&
+                (newColumn.precision !== oldColumn.precision ||
+                    newColumn.scale !== oldColumn.scale)
Evidence
Postgres change detection still compares precision and scale unconditionally, and every detected
changed column enters pre-change cleanup that drops related foreign keys, composite indexes, and
composite unique constraints. Those objects are recreated only in later schema-sync phases, proving
that the query-runner-only guard can still generate drop/create SQL for the phantom difference.

src/driver/postgres/PostgresDriver.ts[1486-1493]
src/schema-builder/RdbmsSchemaBuilder.ts[893-927]
src/schema-builder/RdbmsSchemaBuilder.ts[1229-1283]
src/schema-builder/RdbmsSchemaBuilder.ts[1293-1345]
src/schema-builder/RdbmsSchemaBuilder.ts[227-245]
src/driver/postgres/PostgresQueryRunner.ts[1621-1645]

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

## Issue description
Spatial precision/scale mismatches are ignored only inside `PostgresQueryRunner.changeColumn`, after the schema builder has already dropped related foreign keys, composite indexes, and composite unique constraints. Exclude precision/scale from Postgres spatial-column change detection so these columns do not enter the update flow when no real database property differs.

## Issue Context
`geometry` and `geography` precision/scale do not round-trip through PostgreSQL. Keep comparison of all other column properties, including spatial feature type and SRID, and add coverage for a spatial column participating in a related index or constraint.

## Fix Focus Areas
- src/driver/postgres/PostgresDriver.ts[1486-1493]
- src/driver/postgres/PostgresQueryRunner.ts[1621-1629]
- test/functional/schema-builder/idempotency/spatial-precision/spatial-precision.test.ts[21-30]

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


Grey Divider

Qodo Logo

@alumni

alumni commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

FYI, related PR: #12742 handles something similar but for length and more generically (for all drivers and based on the data types that the driver supports).

I was taking a quick look over that one some time ago and was thinking if we should do the same for precision/scale, then this PR pops up 🙃

We have withPrecisionColumnTypes and withScaleColumnTypes, so we could check if the data type is in there (for each of the comparisons) instead of using the isSpatialColumn check.

…ort it in postgres

Geometry/geography (and other) column types have no precision or scale in
the Postgres catalog, so the value from entity metadata (for geometry, used
only to control ST_AsGeoJSON precision on read) never matches what is
introspected back. findChangedColumns therefore always reported such columns
as changed, causing the schema builder to drop and recreate related foreign
keys, composite indices, and composite unique constraints, and emit a no-op

    ALTER TABLE ... ALTER COLUMN ... TYPE ...

on every synchronize/migration:generate.

Only compare precision when the column type is in withPrecisionColumnTypes,
and scale when it is in withScaleColumnTypes, mirroring how length is handled
generically in typeorm#12742. The same guard is applied to the precision/scale
branch of PostgresQueryRunner.changeColumn.

Closes typeorm#12800

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@mkovac-conductive
mkovac-conductive force-pushed the fix/postgres-spatial-precision-phantom-diff branch from 5a8c652 to 673232c Compare August 27, 2026 11:29
@pkg-pr-new

pkg-pr-new Bot commented Sep 9, 2026

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

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

commit: 673232c

@michaelbromley

Copy link
Copy Markdown
Member

This repository was recently restructured into a pnpm workspace, and the typeorm package moved from the repo root into packages/typeorm (PR #12854). Everything moved as a pure rename with no content changes, so your branch should replay across the move cleanly once git's rename detection limit is raised:

git fetch origin
git -c merge.renameLimit=999999 -c diff.renameLimit=999999 rebase origin/master

If your branch has diverged enough that a full rebase isn't worth it, scripts/migrate-pr-to-packages-layout.sh --replay (run on top of master) rewrites the paths of your net diff instead. Both approaches, plus a path-mapping table and a prompt you can hand to a coding agent, are documented in scripts/migrate-pr-to-packages-layout.md.

Sorry for the disruption, and thanks for your patience.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linked-issue PR references an issue

Development

Successfully merging this pull request may close these issues.

Postgres: geometry column with precision generates an endless phantom migration

3 participants