Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upsertMany error within nullable and composite key reference. #4945

Closed
5 tasks done
z4o4z opened this issue Nov 21, 2023 · 1 comment
Closed
5 tasks done

upsertMany error within nullable and composite key reference. #4945

z4o4z opened this issue Nov 21, 2023 · 1 comment

Comments

@z4o4z
Copy link

z4o4z commented Nov 21, 2023

Describe the bug

ORM generates incorrect Ref for nullable and composite key references.

Here's an example:

class EntityA {
  @Primary()
  id!: string;
  
  @Primary()
  envID!: string;
}

class EntityB {
  @Primary()
  id!: string;
  
  @Primary()
  envID!: string;

  @Property({ type: 'text' })
  name!: string;

  @ManyToOne(() => EntityA, {
    name: 'entity_a_id',
    default: null,
    onDelete: 'set default',
    nullable: true,
    fieldNames: ['entity_a_id', 'env_id'],
  })
  entityA: Ref<EntityA> | null = null;


}

when I try to upsert entities like below, I get an error ValidationError: Value for EntityB.envID is required, 'undefined' found.

await this.em.upsertMany(EntityA, [{ id: 'entity-a-1', envID: 'env-1' }, { id: 'entity-a-2', envID: 'env-1' }]);

await this.em.upsertMany(EntityB, [{ id: 'entity-b-1', envID: 'env-1', name: 'entity-b-1', entityA: ref({ id: 'entity-a-1', envID: 'env-1' }) }, { id: 'entity-b-2', envID: 'env-1', name: 'entity-b-2', entityA: ref({ id: 'entity-a-2', envID: 'env-1' }) }]);

After some debugging, I've found that finalize method in the QueryBuilderHelper adds entity_a_id as a returning field, cause it has default value, I guess that makes sense. But when the returned value is mapped the original entityA ref (that has both id and envID) replaced with incorrect one (that has only id, no env id), it happens cause in the mapReturnedValues entityA is set to entity_a_id that is returned from the DB, but it should be a composite value (array of entity_a_id and envID).

Not sure what's the best solution here, probably ignore nullable + composite key references during mapReturnedValues?

Reproduction

see above

What driver are you using?

@mikro-orm/postgresql

MikroORM version

5.9.4

Node.js version

v16.19.0

Operating system

macOS

Validations

@B4nan
Copy link
Member

B4nan commented Nov 26, 2023

Backported the fix from v6 here: a7e9a82

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants