typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
33.46k stars 6.21k forks source link

upsert() of an Entity with lazy relation results in null column value #10530

Open alexey-pelykh opened 5 months ago

alexey-pelykh commented 5 months ago

Issue description

upsert() of an Entity with lazy relation results in null column value

Expected Behavior

upsert() can recognize value in lazy relation field named otherEntity.

Actual Behavior

upsert() looks for __otherEntity__ field.

Steps to reproduce

await dataSource.entityManager.upsert(
  MyEntity,
  {
    externalReference: '...',
    otherEntity: Promise.resolve(otherEntity),
  },
);

with

@Entity()
export class MyEntity {
   @Column({ unique: true })
  @Index({ unique: true })
  externalReference: string;

  @ManyToOne(() => MyOtherEntity, { lazy: true })
  @JoinColumn()
  otherEntity: Promise<MyOtherEntity>;
};

results in otherEntityId column to be empty because getLazyRelationsPromiseValue is not true thus it expects a __otherEntity__ field: https://github.com/typeorm/typeorm/blob/d184d8598c057ce8fa54815e669b567238f3a86e/src/metadata/RelationMetadata.ts#L474

My Environment

Dependency Version
Operating System macOS
Node.js version 21.1.0
Typescript version 5.1.3
TypeORM version 0.3.17

Additional Context

No response

Relevant Database Driver(s)

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.

alexey-pelykh commented 5 months ago

Working on PR that at least exposes the issue in #10531 10531