Skip to content

upsert incorrectly modified entities #11083

Closed as not planned
Closed as not planned

Description

@wuuli

Issue description

I upsert some data, but i found the entity was modified with other id.

Expected Behavior

Entities was modified currectly.

const effectFiles:  EffectFile[] = [];

// some create entities logic

console.log(effectFiles)
// effectFiles = [
//  {
//    id: 'b293ab544529f62dba35a2568f64dcca',
//    url: 'https://xxxx/b293ab544529f62dba35a2568f64dcca' ,
//  },
//  {
//    id: '10116ad7b236283e72a8f9e00c4004bc',
//    url: 'https://xxxx/10116ad7b236283e72a8f9e00c4004bc' ,
//  },
// ]


await FileRepository.upsert(effectFiles, ['id']);

console.log(effectFiles)
// effectFiles = [
//  {
//    id: 'b293ab544529f62dba35a2568f64dcca', 
//    url: 'https://xxxx/b293ab544529f62dba35a2568f64dcca' ,
//    loaded: false, 
//  },
//  {
//    id: '10116ad7b236283e72a8f9e00c4004bc',
//    url: 'https://xxxx/10116ad7b236283e72a8f9e00c4004bc' ,
//    loaded: false, 
//  },
// ]

Actual Behavior

Entities was modified currectly.

const effectFiles:  EffectFile[] = [];

// some create entities logic

console.log(effectFiles)
// effectFiles = [
//  {
//    id: 'b293ab544529f62dba35a2568f64dcca',
//    url: 'https://xxxx/b293ab544529f62dba35a2568f64dcca' ,
//  },
//  {
//    id: '10116ad7b236283e72a8f9e00c4004bc',
//    url: 'https://xxxx/10116ad7b236283e72a8f9e00c4004bc' ,
//  },
// ]


await FileRepository.upsert(effectFiles, ['id']);

console.log(effectFiles)
// effectFiles = [
//  {
//    id: '10116ad7b236283e72a8f9e00c4004bc',  // **wrong id !!!**
//    url: 'https://xxxx/b293ab544529f62dba35a2568f64dcca' ,
//    loaded: false, 
//  },
//  {
//    id: 'b293ab544529f62dba35a2568f64dcca',  // **wrong id !!!**
//    url: 'https://xxxx/10116ad7b236283e72a8f9e00c4004bc' ,
//    loaded: false, 
//  },
// ]

Steps to reproduce

  1. create some entites
  2. upsert them
  3. some the entites will be incorrectly modified!

My Environment

Dependency Version
Operating System windows
Node.js version 16.17.1
Typescript version 5.6.2
TypeORM version 0.3.20

Additional Context

@Entity('effect_file')
export class EffectFile {
  @PrimaryColumn('varchar')
  id!: string;

  @Column('varchar')
  url!: string;

  @Column('boolean', { default: false })
  loaded!: boolean;
}

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?

Yes, I have the time, but I don't know how to start. I would need guidance.

Activity

  1. wuuli commented on Oct 2, 2024

    @wuuli
    Author

    I found that the reason is the order of returningResult is different with entityIds.

    // src/query-builder/ReturningResultsEntityUpdator.ts
        async insert(
            insertResult: InsertResult,
            entities: ObjectLiteral[],
        ): Promise<void> {
        // ....
        // the order of returningResult is different with entityIds !!!
                const returningResult: any = await this.queryRunner.manager
                    .createQueryBuilder()
                    .select(
                        metadata.primaryColumns.map(
                            (column) =>
                                metadata.targetName + "." + column.propertyPath,
                        ),
                    )
                    .addSelect(
                        insertionColumns.map(
                            (column) =>
                                metadata.targetName + "." + column.propertyPath,
                        ),
                    )
                    .from(metadata.target, metadata.targetName)
                    .where(entityIds)
                    .setOption("create-pojo") // use POJO because created object can contain default values, e.g. property = null and those properties might be overridden by merge process
                    .getMany()
                
                // and the entities were modified incorrectly !!!
                entities.forEach((entity, entityIndex) => {
                    this.queryRunner.manager.merge(
                        metadata.target as any,
                        generatedMaps[entityIndex],
                        returningResult[entityIndex],
                    )
    
                    this.queryRunner.manager.merge(
                        metadata.target as any,
                        entity,
                        returningResult[entityIndex],
                    )
                })
  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.

  3. added this to the 1.0 milestone on Mar 9, 2026
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions