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

Populate throws ValidationError after upgrading from 5.3.1 to 5.4.1 #3490

Closed
co-sic opened this issue Sep 12, 2022 · 1 comment
Closed

Populate throws ValidationError after upgrading from 5.3.1 to 5.4.1 #3490

co-sic opened this issue Sep 12, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@co-sic
Copy link
Contributor

co-sic commented Sep 12, 2022

Describe the bug
After upgrading from v5.3.1 to v5.4.1 of mikro-orm I get a ValidationError while populating an Entity.

Stack trace

ValidationError: Entity 'Contract' does not have property 'contracts'
    at Function.invalidPropertyName (***/node_modules/@mikro-orm/core/errors.js:53:16)
    at SqlEntityManager.preparePopulate (***/node_modules/@mikro-orm/core/EntityManager.js:855:44)
    at SqlEntityManager.lockAndPopulate (***/node_modules/@mikro-orm/core/EntityManager.js:815:39)
    at SqlEntityManager.findOne (***/node_modules/@mikro-orm/core/EntityManager.js:317:22)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at SqlEntityManager.findOneOrFail (***/node_modules/@mikro-orm/core/EntityManager.js:338:22)
    at getUnlockedEmployeesForContract (***/src/services/employeeService/getUnlockedEmployeesForContract.ts:32:22) {
  entity: undefined
}

To Reproduce
Minimal example:

@Entity()
export class Customer extends MikroORMBaseEntity<Customer, 'id'> {
  @PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
  id!: string;

  @OneToMany(() => Contract, (contract) => contract.customer)
  contracts = new Collection<Contract>(this);

  @ManyToOne()
  parentCustomer: Customer | null = null;

  @OneToMany(() => Customer, (customer) => customer.parentCustomer)
  childCustomers = new Collection<Customer>(this);
}

@Entity()
export class Contract extends MikroORMBaseEntity<Contract, 'id'> {
 @PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
  id!: string;

  @ManyToOne()
  customer!: Customer;
}

const contract = await orm.entityManager.getRepository(Contract).findOneOrFail(
      { id: contractId },
      { populate: ['customer.childCustomers.contracts'] },
    );

It seems like it is detecting the childCustomer property as a Contract, even though it is a Customer.
If needed i can provide a working minimal example repository.

Versions

Dependency Version
node v18.8.0
typescript 4.8.3
mikro-orm 5.4.1
@B4nan B4nan added the bug Something isn't working label Sep 12, 2022
@B4nan
Copy link
Member

B4nan commented Sep 12, 2022

Thanks, here is a failing test case based on this:

import { Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey } from '@mikro-orm/core';
import type { SqliteDriver } from '@mikro-orm/sqlite';

@Entity()
export class Contract {

  @PrimaryKey()
  id!: number;

  @ManyToOne('Customer')
  customer!: any;

}

@Entity()
export class Customer {

  @PrimaryKey()
  id!: number;

  @OneToMany(() => Contract, contract => contract.customer)
  contracts = new Collection<Contract>(this);

  @ManyToOne({ entity: 'Customer', nullable: true })
  parentCustomer?: Customer;

  @OneToMany(() => Customer, customer => customer.parentCustomer)
  childCustomers = new Collection<Customer>(this);

}

let orm: MikroORM<SqliteDriver>;

beforeAll(async () => {
  orm = await MikroORM.init({
    entities: [Contract, Customer],
    dbName: ':memory:',
    type: 'sqlite',
  });
  await orm.schema.createSchema();
});

afterAll(async () => {
  await orm.close(true);
});

test(`GH issue 3490`, async () => {
  const c = orm.em.create(Customer, {
    childCustomers: [
      {
        contracts: [
          { id: 1 },
          { id: 2 },
          { id: 3 },
        ],
      },
    ],
  });
  await orm.em.fork().persist(c).flush();

  const contract = await orm.em.findOneOrFail(Contract, c.id,
    { populate: ['customer.childCustomers.contracts'] },
  );

  expect(contract.customer.childCustomers[0].contracts).toHaveLength(3);
});

@B4nan B4nan closed this as completed in e3c835a Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants