Skip to content

Populating Nested Field returns the wrong number of associated entities #144

@thiagomini

Description

@thiagomini

Describe the bug

  • Given we are using Entity Schema definitions
  • And we have a Company entity class with its schema
  • And a Company has many User's
  • And a User has many Profile's
  • And we have a Company "Acme" that has "User1" and "User2" as users
  • And "User1" has two profiles
  • And "User2" has one profile
  • When we try to fetch the Company from the database, populating both user and profile
  • Then the "User2" has three profiles associated.

The Diagram below illustrates that:

flowchart LR
    A[Company 'A'] -->|Has| B(User 1)
    A[Company 'A'] -->|Has| C(User 2)
    B --> |Has| D(Profile 1)  
    B --> |Has| E(Profile 2)  
    C --> |Has| F(Profile 3)
Loading

Stack trace

AssertionError: expected { Object (0, 1, ...) } to have a length of 1 but got 3

- Expected
+ Received

- 1
+ 3

 ❯ src/mikro-orm/mikro-orm-internal.module.spec.ts:100:45
     98|     // Assert
     99|     const userWithASingleProfile = companyWithProfiles.users.find(u => u.id === user2.id);
    100|     expect(userWithASingleProfile.profiles).toHaveLength(1);
       |                                             ^
    101|   });
    102| 

To Reproduce
Steps to reproduce the behavior:

  1. Clone the minimum reproducible code repository: https://github.com/thiagomini/nest-mikro-orm-example/tree/bug/nested-populated-entities
  2. run yarn
  3. run docker compose up -d
  4. run yarn test

Expected behavior
"User2" should have only ONE associated Profile.

Additional Information

💡 Link to the test case

Versions

Dependency Version
node 18.12.1
typescript 5.2.2
mikro-orm 5.8.3
pg 8.11.2

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions