簡體   English   中英

TypeORM getManyAndCount() 不返回關系數據

[英]TypeORM getManyAndCount() doesn't return relation data

我對 TypeORM 和 NestJs 很陌生。

在這個項目中,我有一個與 VehicleBrand 具有 ManyToOne 關系的實體 VehicleModel。

當我在查詢中運行 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(); //<--- Here!

    return new GenericPagedListDto<E>(list, total);
}

這是我的關系

車輛模型.entity.ts:

@ManyToOne(() => VehicleBrand, vehicleBrand => vehicleBrand.vehicleModels)
@JoinColumn({ name: 'vehicle_brand_id' })
vehicleBrand: VehicleBrand;

車輛品牌.entity.ts:

@OneToMany(() => VehicleModel, vehicleModel => vehicleModel.vehicleBrand)
vehicleModels: VehicleModel[];

以這種方式使用Eager 關系

@OneToMany(() => VehicleModel, vehicleModel => vehicleModel.vehicleBrand, { eager: true })
vehicleModels: VehicleModel[];

有了這個,關系就被加載了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM