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

Undetected Self Referencing Discriminators #2364

Closed
parisholley opened this issue Nov 6, 2021 · 1 comment
Closed

Undetected Self Referencing Discriminators #2364

parisholley opened this issue Nov 6, 2021 · 1 comment

Comments

@parisholley
Copy link
Contributor

Given the following mapping:

@Entity({ tableName: 'base', discriminatorColumn: 'type', abstract: true })
export default abstract class BaseMikro {
  @ManyToOne(() => BaseMikro, { fieldName: 'parentId', wrappedReference: true })
  parent: IdentifiedReference<ActivityMikro<any>>;
}

This is the compiled output function

  if (data.parent === null) {
    entity.parent = null;
  } else if (typeof data.parent !== 'undefined') {
    if (isPrimaryKey(data.parent, true)) {
      entity.parent = new Reference(factory.createReference('BaseMikro', data.parent, { merge: true, convertCustomTypes }));
    } else if (data.parent && typeof data.parent === 'object') {
      entity.parent = new Reference(factory.create('BaseMikro', data.parent, { initialized: true, merge: true, newEntity, convertCustomTypes }));
    }
  }

Given the following data

1: parent = null, type = 'Impl1'
2: parent = 1, type = 'Impl2'

When I run a query like the one below:

const implementations = em.find(BaseMikro, {});

assert(implementations[0] instanceof Impl1); // pass
assert(implementations[1] instanceof Impl2); // fail (is BaseMikro)

The generated code above doesn't seem to understand it is a discriminated class.

Versions

    "@mikro-orm/cli": "^4.5.9",
    "@mikro-orm/core": "^4.5.9",
    "@mikro-orm/migrations": "^4.5.9",
    "@mikro-orm/nestjs": "^4.3.1",
    "@mikro-orm/postgresql": "^4.5.9",
    "@mikro-orm/reflection": "^4.5.9",
@B4nan
Copy link
Member

B4nan commented Nov 6, 2021

Please provide full reproduction for this, this is not enough to reproduce. For example this test basically checks the same.

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