Skip to content

fix: guard metadata access in InsertQueryBuilder for raw-table upsert - #12702

Merged
alumni merged 1 commit into
typeorm:masterfrom
xianjianlf2:fix/insert-orupdate-raw-table-metadata-11549
Jul 22, 2026
Merged

alumni merged 1 commit into
typeorm:masterfrom
xianjianlf2:fix/insert-orupdate-raw-table-metadata-11549

Conversation

@xianjianlf2

Copy link
Copy Markdown
Contributor

Description

Fixes #11549. Building an upsert against a raw table name throws before the query can be generated:

dataSource.createQueryBuilder()
  .insert().into("some_table", ["id", "name"])
  .values({ id: 1, name: "a" })
  .orUpdate(["name"], ["id"])
  .getSql()
// TypeORMError: Cannot get entity metadata for the given alias "some_table"

In InsertQueryBuilder.createInsertExpression(), after assembling the ON CONFLICT ... DO UPDATE SET columns, the code unconditionally read this.expressionMap.mainAlias!.metadata.columns to append UpdateDateColumn = DEFAULT entries. A raw table has no entity metadata, so Alias.metadata's getter throws. Every other metadata access in this method (the SQLite JSON branch, the MSSQL IDENTITY_INSERT branch, getInsertedColumns) is already guarded by hasMetadata; this was the lone unguarded read.

Fix

Wrap the update-date-column block in if (this.expressionMap.mainAlias!.hasMetadata) { … }, matching the method's established guard pattern. A raw table has no UpdateDateColumn, so there is nothing to append.

Tests

Added a DB-free test to test/functional/query-builder/insert-on-conflict/ (per the compliance checklist, with the issue reference in a comment) that builds the raw-table upsert and asserts getSql() contains ON CONFLICT ( "id" ) DO UPDATE SET "name" = EXCLUDED."name". Runs on the in-memory better-sqlite3 harness (same on-conflict-do-update path as Postgres). Verified break→red (Cannot get entity metadata … InsertQueryBuilder.ts) → restore→green; on-conflict describe 8/8 passing.

Closes #11549

@github-actions github-actions Bot added the linked-issue PR references an issue label Jul 16, 2026
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

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

Previous review results

Review updated until commit a887a6e ⚖️ Balanced

Results up to commit c7740ea


🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Great, no issues found!

Qodo reviewed your code and found no material issues that require review
Results up to commit 99baad6


🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Great, no issues found!

Qodo reviewed your code and found no material issues that require review
Qodo Logo

@pkg-pr-new

pkg-pr-new Bot commented Jul 17, 2026 •

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

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

commit: a887a6e

@Cprakhar
Cprakhar requested review from alumni and gioboa July 17, 2026 18:24
@xianjianlf2
xianjianlf2 force-pushed the fix/insert-orupdate-raw-table-metadata-11549 branch from 99baad6 to c7740ea Compare July 22, 2026 02:59
@xianjianlf2

Copy link
Copy Markdown
Contributor Author

Rebased this branch onto the latest master and pushed the updated commit (c7740ea).

Local checks:

  • PATH=/Users/even/.nvm/versions/node/v24.18.0/bin:$PATH pnpm run compile
  • PATH=/Users/even/.nvm/versions/node/v20.20.2/bin:$PATH pnpm exec mocha --no-config --check-leaks --exit --file ./build/compiled/test/utils/test-setup.js --timeout 90000 build/compiled/test/functional/query-builder/insert-on-conflict/query-builder-insert-on-conflict.test.js (8 passing)

Note: I kept the unrelated untracked local package-lock.json untouched.

… tables

When building an INSERT ... ON CONFLICT DO UPDATE SET query for a raw
table name passed to .into() (no entity metadata), createInsertExpression
unconditionally read mainAlias.metadata.columns to append UpdateDateColumn
"= DEFAULT" clauses, throwing "Cannot get entity metadata for the given
alias". A raw table has no UpdateDateColumns, so guard the access with
hasMetadata, matching the surrounding metadata-dependent branches.

Closes typeorm#11549
@xianjianlf2
xianjianlf2 force-pushed the fix/insert-orupdate-raw-table-metadata-11549 branch from c7740ea to a887a6e Compare July 22, 2026 09:50
@xianjianlf2

Copy link
Copy Markdown
Contributor Author

Rebased this PR onto current master (008f5c6).

Local verification after the rebase:

  • git diff --check
  • pnpm run compile
  • pnpm exec mocha build/compiled/test/functional/query-builder/insert-on-conflict/query-builder-insert-on-conflict.test.js --grep "raw table without entity metadata" (1 passing)

@alumni
alumni merged commit 281bfba into typeorm:master Jul 22, 2026
46 checks passed
@github-actions github-actions Bot added this to the 2.0.next milestone Jul 22, 2026
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.

Issue with OnUpdate on QueryBuilder

4 participants