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

Adding fields in find() will produce extra SQL even LoadStrategy is JOINED #1707

Closed
michael-vascue opened this issue Apr 19, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@michael-vascue
Copy link

michael-vascue commented Apr 19, 2021

Describe the bug
Adding fields in find() will produce extra SQL even LoadStrategy is JOINED.
Is it the expected behaviour?

Stack trace
N/A

To Reproduce
FYR: Initialise repo

constructor(
    @InjectRepository(User)
    private readonly userRepository: EntityRepository<User>,
}

Without fields will produce 1 query

await this.userRepository.find({}, {
      populate: ['roles'],
      strategy: LoadStrategy.JOINED,
      limit: 1,
});
/* Output:
select `e0`.`id`, `r1`.`id` as `r1__id`
from `user` as `e0` 
left join `user_roles` as `e2` on `e0`.`id` = `e2`.`user_id` 
left join `role` as `r1` on `e2`.`role_id` = `r1`.`id` limit 1
*/

With fields will produce 2 queries

await this.userRepository.find({}, {
      populate: ['roles'],
      fields: ['id', 'roles.id'], // id = users.id
      strategy: LoadStrategy.JOINED,
      limit: 1,
});
/* Output
select `e0`.`id` from `user` as `e0` limit 1 [took 5 ms]
select `e0`.`id`, `e1`.`role_id` as `fk__role_id` from `role` as `e0` left join `user_roles` as `e1` on `e0`.`id` = `e1`.`role_id` where `e1`.`user_id` in (7) limit 1 [took 6 ms]
*/

Expected behavior
Expected to produce 1 SQL whenever fields property is provided or not.

Additional context
N/A

Versions

Dependency Version
node 14.15.0
typescript 4.2.4
mikro-orm 4.5.3
@mikro-orm/mysql 4.5.3
@mikro-orm/nestjs 4.2.0
@B4nan
Copy link
Member

B4nan commented Apr 20, 2021

Yes, this combination is currently not implemented. Btw you can't use limit here if you want to use joined strategy and load to-many relation that way. You'd have to use QueryFlag.PAGINATE if you really need to restrict the SQL query limit, otherwise it will never select more than one entity at a level (so you would always end up with one role at most).

@B4nan B4nan added the enhancement New feature or request label Apr 20, 2021
@B4nan B4nan added this to the 5.0 milestone Apr 20, 2021
@michael-vascue
Copy link
Author

Yes, this combination is currently not implemented. Btw you can't use limit here if you want to use joined strategy and load to-many relation that way. You'd have to use QueryFlag.PAGINATE if you really need to restrict the SQL query limit, otherwise it will never select more than one entity at a level (so you would always end up with one role at most).

Okay and Thanks for the recommendation.
BTW, MikroORM is great and we are using it to develop our product.

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

No branches or pull requests

2 participants