Skip to content

fix(postgres,cockroachdb): compare spatial columns against their default feature type and SRID - #12812

Merged
alumni merged 2 commits into
typeorm:masterfrom
samuelmbabhazi:fix/spatial-default-comparison
Oct 7, 2026
Merged

alumni merged 2 commits into
typeorm:masterfrom
samuelmbabhazi:fix/spatial-default-comparison

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Contributor

Description of change

Fixes #12811

A geometry or geography column declared without spatialFeatureType or srid is created as plain geometry, which PostGIS and CockroachDB report back as feature type Geometry with SRID 0. findChangedColumns compared those values with the undefined metadata, so every such column produced the same no-op ALTER COLUMN ... TYPE on each migration:generate. The repository's own spatial Post test entity showed this drift: three phantom queries on both databases.

The comparison now applies the defaults the decorator reference already documents (feature type Geometry, SRID 0) on both sides, in the Postgres and CockroachDB drivers alike. Removing a feature type or SRID from an entity still converges: the stored value then differs from the default, one ALTER COLUMN ... TYPE geometry is generated, and the next run reports no changes.

Tests: a new schema-builder/idempotency/spatial-defaults case (postgres and cockroachdb) asserts no pending queries for such columns, and the idempotency assertions of the spatial suites, previously scoped to the dimensional table because of this drift, now cover the whole schema. Reverting the driver change makes exactly those three assertions fail with the three phantom queries. The idempotency and schema builder suites pass (155 tests), as do the spatial suites (27).

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): no documentation change, the documented defaults are now honoured

@github-actions github-actions Bot added the linked-issue PR references an issue label Aug 28, 2026
@pkg-pr-new

pkg-pr-new Bot commented Aug 28, 2026 •

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

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

commit: dff2548

@qodo-free-for-open-source-projects

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

Copy link
Copy Markdown

Code Review by Qodo

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

Grey Divider


Action required

1. Postgres ignores default spatial edits 🐞 Bug ≡ Correctness
Description
findChangedColumns maps omitted spatial options and explicit "Geometry"/0 options to the same
values even though createFullType emits bare and parameterized declarations and CockroachDB
introspection exposes that distinction through crdb_sql_type. When an entity changes between
either declaration in PostgreSQL or CockroachDB, the column is filtered out before changeColumn
reaches the spatial type alteration path, leaving the existing database declaration unchanged.
Code

packages/typeorm/src/driver/postgres/PostgresDriver.ts[R1515-1519]

+                (tableColumn.spatialFeatureType ?? "geometry").toLowerCase() !==
+                    (
+                        columnMetadata.spatialFeatureType ?? "geometry"
+                    ).toLowerCase() ||
+                (tableColumn.srid ?? 0) !== (columnMetadata.srid ?? 0) ||
Evidence
The cited driver comparison logic normalizes missing spatial metadata to explicit Geometry and
SRID 0, erasing whether the declaration was bare or parameterized; CockroachDB's introspection
already obtains crdb_sql_type, which can preserve that distinction. The full-type generation code
emits different SQL for absent and explicitly supplied options, while the cited query-runner paths
issue the required ALTER COLUMN TYPE only when the column first survives changed-column detection.

packages/typeorm/src/driver/postgres/PostgresDriver.ts[1381-1388]
packages/typeorm/src/driver/postgres/PostgresDriver.ts[1487-1519]
packages/typeorm/src/driver/postgres/PostgresQueryRunner.ts[3943-3989]
packages/typeorm/src/driver/postgres/PostgresQueryRunner.ts[2325-2347]
packages/typeorm/src/driver/cockroachdb/CockroachDriver.ts[920-946]
packages/typeorm/src/driver/cockroachdb/CockroachDriver.ts[1039-1074]
packages/typeorm/src/driver/cockroachdb/CockroachQueryRunner.ts[2149-2171]
packages/typeorm/src/driver/cockroachdb/CockroachQueryRunner.ts[3658-3688]

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

## Issue description
PostgreSQL and CockroachDB change detection collapse bare spatial declarations and explicit `Geometry,0` declarations even though the drivers generate different SQL for them and CockroachDB exposes its declared type through `crdb_sql_type`. Preserve whether the introspected database type is unconstrained so explicit default-valued spatial options can be added or removed.
## Fix Focus Areas
- packages/typeorm/src/driver/postgres/PostgresDriver.ts[1512-1519]
- packages/typeorm/src/driver/postgres/PostgresQueryRunner.ts[3943-3989]
- packages/typeorm/src/driver/postgres/PostgresDriver.ts[1381-1388]
- packages/typeorm/src/driver/cockroachdb/CockroachDriver.ts[1067-1074]
- packages/typeorm/src/driver/cockroachdb/CockroachDriver.ts[920-946]
- packages/typeorm/src/driver/cockroachdb/CockroachQueryRunner.ts[3658-3688]
## Recommended Fix
Use each database's introspected spatial type declaration, including CockroachDB's `crdb_sql_type`, to leave spatial feature type and SRID undefined for a bare `geometry` or `geography` declaration while retaining explicit `Geometry,0` values for parameterized declarations. Compare those preserved forms directly instead of replacing missing values with defaults, and add PostgreSQL and CockroachDB transition tests covering changes in both directions between bare and explicit `Geometry,0` declarations.

ⓘ 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 route each severity your way: inline, summary, both, or drop

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@Cprakhar Cprakhar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix 🚀

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

…ult feature type and SRID

A geometry or geography column declared without spatialFeatureType or
srid is created as plain geometry, which the database reports back as
Geometry with SRID 0. The change detection compared those values with
the undefined metadata, so every such column produced the same no-op
ALTER COLUMN TYPE on each migration:generate.
@samuelmbabhazi
samuelmbabhazi force-pushed the fix/spatial-default-comparison branch from 964ad8c to 34bc31f Compare September 18, 2026 17:12
@samuelmbabhazi

Copy link
Copy Markdown
Contributor Author

Rebased onto the packages/ layout as described, with a single commit. The diff is unchanged apart from the paths, and the spatial and schema-builder suites pass on PostgreSQL and CockroachDB locally.

@alumni
alumni merged commit a5bd2e5 into typeorm:master Oct 7, 2026
46 checks passed
@github-actions github-actions Bot added this to the 2.0.next milestone Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Postgres/CockroachDB: spatial columns without spatialFeatureType or srid generate an endless no-op migration

5 participants