I'm completely new to TypeORM and NestJs.
Currently, I am working on a project where I have an entity called VehicleModel which has a ManyToOne relationship with VehicleBrand.
However, when I execute getManyAndCount() on my query, I am puzzled as to why I am not receiving the VehicleBrand data. I double-checked this by running getQuery and thoroughly inspecting the code.
Interestingly, this same code functions perfectly fine with other entities that have relationships:
public async findAll(findDto: F): Promise<GenericPagedListDto<E>> {
const query = this.createQueryBuilder('t');
await this.createCriteria(query, findDto);
if (findDto.page > 0) {
query.skip((findDto.page - 1) * findDto.limit);
}
query.take(+findDto.limit);
const sort = findDto.sort ? JSON.parse(findDto.sort) : undefined;
if (sort) {
Object.keys(sort).forEach((key) => {
query.orderBy(`${!key.includes(".")?"t.":""}${key}`, sort[key]==="asc"?"ASC":"DESC");
});
}
const [list, total] = await query.getManyAndCount(); //<--- This line!
return new GenericPagedListDto<E>(list, total);
}
Here is how my relation is defined:
vehicle-model.entity.ts:
@ManyToOne(() => VehicleBrand, vehicleBrand => vehicleBrand.vehicleModels)
@JoinColumn({ name: 'vehicle_brand_id' })
vehicleBrand: VehicleBrand;
vehicle-brand.entity.ts:
@OneToMany(() => VehicleModel, vehicleModel => vehicleModel.vehicleBrand)
vehicleModels: VehicleModel[];