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

joinAndSelect does not include selected data in output #4034

Closed
tonyjaimep opened this issue Feb 9, 2023 · 3 comments
Closed

joinAndSelect does not include selected data in output #4034

tonyjaimep opened this issue Feb 9, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@tonyjaimep
Copy link
Contributor

Describe the bug
joinAndSelect fields with ManyToOne relationships are not populated. Instead their PKs take their place.

Stack trace
None available

To Reproduce
Steps to reproduce the behavior:

  1. In an EntityRepository add a method that uses qb() and joinAndSelect().
  2. Use joinAndSelect to join and select a relationship (let's call it a).
  3. Use joinAndSelect to join a select a manyToOne relationship field from that relationship (let's call it a.b).
  4. Call the query.
  5. a.b is not populated. Its primary key takes its place.

An example repository that does this is the one used in the example repository
https://github.com/tonyjaimep/mikro-orm-join-demo

import { EntityRepository } from '@mikro-orm/postgresql';
import { Author } from './author.entity';

export class AuthorRepository extends EntityRepository<Author> {
  async findAllWithRelations(userUuid: string): Promise<Author[]> {
    return this.qb()
      .select('*')
      .joinAndSelect('books', 'b')
      .joinAndSelect('b.template', 'bt')
      .leftJoinAndSelect('b.comments', 'bc', {
        'bc.userUuid': userUuid,
      });
  }
}

Expected behavior

Expected output (up until v5.5.3)

template and comments are populated

[
  {
    "uuid": "00000000-0000-0000-0000-000000000000",
    "name": "Test Author 1",
    "books": [
      {
        "uuid": "44444444-4444-4444-4444-444444444444",
        "title": "Test Book 1",
        "template": {
          "uuid": "22222222-2222-2222-2222-222222222222",
          "foo": "Test Book Template 1"
        },
        "author": "00000000-0000-0000-0000-000000000000",
        "comments": [
          {
            "uuid": "7d36d725-ab94-4324-b8ce-f2fac30964c2",
            "userUuid": "88888888-8888-8888-8888-888888888888",
            "message": "Test comment 2",
            "book": "44444444-4444-4444-4444-444444444444"
          },
          {
            "uuid": "b35373bb-fb4e-4818-bc58-8ffe691d25c7",
            "userUuid": "88888888-8888-8888-8888-888888888888",
            "message": "Test comment 1",
            "book": "44444444-4444-4444-4444-444444444444"
          }
        ]
      }
    ]
  },
  {
    "uuid": "11111111-1111-1111-1111-111111111111",
    "name": "Test Author 2",
    "books": [
      {
        "uuid": "55555555-5555-5555-5555-555555555555",
        "title": "Test Book 2",
        "template": {
          "uuid": "33333333-3333-3333-3333-333333333333", 
          "foo": "Test Book Template 2"
        },
        "author": "11111111-1111-1111-1111-111111111111",
        "comments": [
          {
            "uuid": "9bc6a377-4431-424c-8da7-1e3c15a3c91d",
            "userUuid": "88888888-8888-8888-8888-888888888888",
            "message": "Test comment 4",
            "book": "55555555-5555-5555-5555-555555555555"
          },
          {
            "uuid": "8ce51989-ce3e-4ac6-973d-1941d03870b9",
            "userUuid": "88888888-8888-8888-8888-888888888888",
            "message": "Test comment 3",
            "book": "55555555-5555-5555-5555-555555555555"
          }
        ]
      }
    ]
  }
]
Actual behavior (as of v5.6.0)

template is not populated, but comments are

[
  {
    "uuid": "00000000-0000-0000-0000-000000000000",
    "name": "Test Author 1",
    "books": [
      {
        "uuid": "44444444-4444-4444-4444-444444444444",
        "title": "Test Book 1",
        "template": "22222222-2222-2222-2222-222222222222",
        "author": "00000000-0000-0000-0000-000000000000",
        "comments": [
          {
            "uuid": "7d36d725-ab94-4324-b8ce-f2fac30964c2",
            "userUuid": "88888888-8888-8888-8888-888888888888",
            "message": "Test comment 2",
            "book": "44444444-4444-4444-4444-444444444444"
          },
          {
            "uuid": "b35373bb-fb4e-4818-bc58-8ffe691d25c7",
            "userUuid": "88888888-8888-8888-8888-888888888888",
            "message": "Test comment 1",
            "book": "44444444-4444-4444-4444-444444444444"
          }
        ]
      }
    ]
  },
  {
    "uuid": "11111111-1111-1111-1111-111111111111",
    "name": "Test Author 2",
    "books": [
      {
        "uuid": "55555555-5555-5555-5555-555555555555",
        "title": "Test Book 2",
        "template": "33333333-3333-3333-3333-333333333333",
        "author": "11111111-1111-1111-1111-111111111111",
        "comments": [
          {
            "uuid": "9bc6a377-4431-424c-8da7-1e3c15a3c91d",
            "userUuid": "88888888-8888-8888-8888-888888888888",
            "message": "Test comment 4",
            "book": "55555555-5555-5555-5555-555555555555"
          },
          {
            "uuid": "8ce51989-ce3e-4ac6-973d-1941d03870b9",
            "userUuid": "88888888-8888-8888-8888-888888888888",
            "message": "Test comment 3",
            "book": "55555555-5555-5555-5555-555555555555"
          }
        ]
      }
    ]
  }
]

Additional context
This behavior started with v5.6.0. Specifically with bfa4962, which tried to solve #3812.

Versions

Dependency Version
node 16.15
typescript 4.7.4
mikro-orm 5.6.x
your-driver PostgreSQL
@B4nan
Copy link
Member

B4nan commented Feb 9, 2023

Thanks for the repro. The code you mentioned here looks very similar to the test case I've added in that problematic commit, maybe we could just extend it? Looks like it joins two properties on that collection, maybe it's caused by that?

One workaround now would be explicit serialization.

@B4nan
Copy link
Member

B4nan commented Feb 9, 2023

Hmm looks like the test we have there is reproducing this too, it only asserts the collection is populated, not the nested relations inside it.

@B4nan B4nan added the bug Something isn't working label Feb 9, 2023
@B4nan B4nan closed this as completed in 4025869 Feb 9, 2023
@tonyjaimep
Copy link
Contributor Author

Thank you! 🚀

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