TypeORM getManyAndCount()无法返回关联VehicleBrand数据求助
TypeORM + NestJS 关联查询异常:getManyAndCount()无法加载关联的VehicleBrand数据
我是TypeORM和NestJS的新手。项目中VehicleModel实体与VehicleBrand实体配置了ManyToOne关联,但调用getManyAndCount()方法时无法获取到VehicleBrand的数据,已经通过getQuery和代码调试确认了这个问题。其他带有关联关系的实体使用完全相同的通用查询代码却能正常运行,相关代码和关联定义如下:
通用查询代码
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(); //<--- 问题所在! return new GenericPagedListDto<E>(list, total); }
关联关系定义
VehicleModel 实体(vehicle-model.entity.ts)
@ManyToOne(() => VehicleBrand, vehicleBrand => vehicleBrand.vehicleModels) @JoinColumn({ name: 'vehicle_brand_id' }) vehicleBrand: VehicleBrand;
VehicleBrand 实体(vehicle-brand.entity.ts)
@OneToMany(() => VehicleModel, vehicleModel => vehicleModel.vehicleBrand) vehicleModels: VehicleModel[];
内容的提问来源于stack exchange,提问作者Sonrimos
相关产品推荐
相关产品推荐

