Skip to content

ReturningResultsEntityUpdator assigning incorrect uuids to entities after upsert #10574

Closed as not planned

Description

@BARNZ

Issue description

ReturningResultsEntityUpdator assigning incorrect uuids back to entities after upsert

Expected Behavior

ReturningResultsEntityUpdator should build a query to fetch the inserted uuids in an order that matches the order the entities were upserted in.

Actual Behavior

Order is undefined on the query that ReturningResultsEntityUpdator uses to fetch the inserted uuids:

https://github.com/typeorm/typeorm/blob/d184d8598c057ce8fa54815e669b567238f3a86e/src/query-builder/ReturningResultsEntityUpdator.ts#L230C20-L230C20)

It assumes the results are returned in the order they were just inserted - which is not the case at least for Sql.js and SQLite that I have tested against (see screenshot below)

As a result when it loops the entities and tries to match the result set by index, the order of the arrays are different so it assigns the wrong uuid back to each entity.

Steps to reproduce

  1. Have a simple table with uuid as a primary key.
  2. Perform an upsert (on uuid) of several rows into this table.
  3. After the upsert has been run, ReturningResultsEntityUpdator will try to fetch the uuids from the database for these newly updated/inserted rows
  4. The query it builds will not have an order defined, so returned results will not match the order entities were upserted in.
  5. Wrong uuid will be assigned back to the entity

My Environment

TypeOrm v0.3.17
Sql.js 1.8.0

Additional Context

Code in question that produces a result set from the database and assumes the entityIndex aligns in both the result and the entities array. This is not the case as the 2 arrays are ordered differently (at least for sql.js)

image

Comparing the first 5 items of both arrays shows different ordering - so matching by index in both arrays results in different UUIDs:

Screenshot 2023-12-29 101718

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. BARNZ commented on Dec 28, 2023

    @BARNZ
    Author

    Potentially relates to #7094

    I think this bug was introduced by this change: 4fc4a1b

    The query needs to be ordered (somehow?) so that results can be looped over predictably

  2. github-actions commented on Sep 18, 2025

    @github-actions

    TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).

    As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.

    🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
    If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.

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