Skip to content

Issue with OnUpdate on QueryBuilder #11549

Closed

Description

@dylan402

Issue description

Error when using QueryBuilder and onUpdate with postgres driver

Expected Behavior

INSERT INTO "public"."category"("id", "name")
VALUES ($1, $2), ($3, $4)
ON CONFLICT ( "id" ) DO UPDATE SET "name" = EXCLUDED."name"

Actual Behavior

TypeORMError: Cannot get entity metadata for the given alias "public.category"

Steps to reproduce

await dataSource.manager
	.createQueryBuilder()
	.insert()
	.into('public.category', ['id', 'name'])
	.values([
	  { id: 1, name: 'Category 1' },
	  { id: 2, name: 'Category 2' },
	])
	.orUpdate(['name'], ['id'])
	.execute();

My Environment

Dependency Version
Operating System Windows 11 Pro 24H2
Node.js version 20.18.1
Typescript version 5.8.3
TypeORM version 0.3.25

Additional Context

When using the QueryBuilder without an entity, an error occurs due to PR #10458.

Currently, the issue happens on line 579, where it tries to access metadata that doesn’t exist.

Adding a check to validate the existence of the metadata on line 577 prevents the error

if (this.expressionMap.mainAlias!.hasMetadata)

But I’m not sure if that’s the correct solution.

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Activity

  1. 0xkhass commented on Apr 6, 2026

    @0xkhass

    I read through the code and can confirm the root cause. And to answer your question: yes, your propsed solution is exactly right.

    The error is caused caused by an unguarded .metadata

    access in the InsertQueryBuilder#createInsertExpression (the orUpdate path).

    Alias.metadata is intentionally unsafe and throws when no entity metadata is present.

    For raw table names, mainAlias.hasMetadata === false, so:

    this.expressionMap.mainAlias.metadata.columns

    throws an error.

    This block attempts to appends updateDate columns to the DO UPDATE SET clause, but does not check hasMetadata,
    unlike other parts of InsertQueryBuilder.

    Fix:
    Wrapping the block in the guard you suggested solves the issue safely:

    if (this.expressionMap.mainAlias.hasMetadata) {
         updatePart.push(
          ...this.expressionMap.mainAlias.metadata.columns
                 .filter(column => column.isUpdateDate)
                 .map(column => `${this.escape(column.databaseName)} = DEFAULT`)
         )
    }

    Aligns with existing hasMetadata guards in execute() in the same file, and preserves raw table usage.
    May be worth auditing other onUpdate paths for similar cases.

    Happy to open a PR for this if my approach is confirmed! (:

  2. added 2 commits that reference this issue on Jul 22, 2026
    c7740ea
    a887a6e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions