You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** from nest.js service */import{LoadStrategy,MikroORM,wrap}from'@mikro-orm/core';import{EntityRepository,SqlEntityManager}from'@mikro-orm/mysql';import{Injectable}from'@nestjs/common';import{InjectRepository}from'@mikro-orm/nestjs';import{ElistEntity,UserEntity}from'../entities';import{IElist,IElistWithOwnerInfoDTO,IUser,}from'@azure-function-api-mysql/interfaces';
@Injectable()exportclassElistService{constructor(// private readonly mikro: MikroORM,// private readonly em: SqlEntityManager,
@InjectRepository(ElistEntity)privatereadonlyer: EntityRepository<ElistEntity>){}publicasyncfindAll(): Promise<IElistWithOwnerInfoDTO[]>{returnawaitthis.er.findAll({populate: ['owner'],fields: ['*','owner.id','owner.firstName','owner.lastName'],strategy: LoadStrategy.JOINED,});}}
If I run use findAll as shown above, the following query is fired off:
select `e0`.`id`, `o1`.`id` as `o1__id`, `o1`.`first_name` as `o1__first_name`, `o1`.`last_name` as `o1__last_name` from `elists` as `e0` left join `users` as `o1` on `e0`.`owner_id` = `o1`.`id` [took 1 ms]
As you can see in the query, only id is selected from elists table, even though I've specified '*' in fields.
If I change strategy to LoadStrategy.SELECT, then two select queries are fired as expected. However, unexpectedly, a different set of fields is selected (query shown below):
select `e0`.`id`, `e0`.*, `e0`.`owner_id` from `elists` as `e0` [took 2 ms]
[query] select `u0`.`id`, `u0`.`first_name`, `u0`.`last_name` from `users` as `u0` where `u0`.`id` in (X'66c41ebc7bf811ed88765b0e34d8a2a2', X'66c425927bf811ed88765b0e34d8a2a2', X'66c4289e7bf811ed88765b0e34d8a2a2', X'66c42a067bf811ed88765b0e34d8a2a2') order by `u0`.`id` asc
Stack trace
None
To Reproduce
Steps to reproduce the behavior:
Create the above entities, create db and seed with some data (one row in each table should do)
Run EntityRepository.findAll or EntityManager.findAll as shown above with the 2 different strategies
Expected behavior
I expected the only difference between the two strategies to be whether multiple selects run or if a join statement is used. Not the fields that are selected. I.e. I was expecting the LoadStrategy.JOINED query to have the same selected fields as LoadStrategy.SELECT.
Additional context
I'm using nest-js.
Versions
mikro-orm: ^5.6.0
nest-js: ^8.0.0
ts: ~4.8.2
mysql2: ^2.3.3
The text was updated successfully, but these errors were encountered:
B4nan
changed the title
EntityRepository.findAll has inconsistent behavior with LoadStrategy.JOINED
Partial loading of * with LoadStrategy.JOINED
Dec 25, 2022
Describe the bug
I'm running the same function (findAll) with different strategies and getting different results.
My entities:
The query:
If I run use findAll as shown above, the following query is fired off:
As you can see in the query, only id is selected from elists table, even though I've specified '*' in fields.
If I change strategy to LoadStrategy.SELECT, then two select queries are fired as expected. However, unexpectedly, a different set of fields is selected (query shown below):
Stack trace
None
To Reproduce
Steps to reproduce the behavior:
Expected behavior
I expected the only difference between the two strategies to be whether multiple selects run or if a join statement is used. Not the fields that are selected. I.e. I was expecting the LoadStrategy.JOINED query to have the same selected fields as LoadStrategy.SELECT.
Additional context
I'm using nest-js.
Versions
The text was updated successfully, but these errors were encountered: