Typeorm .loadRelationCountAndMap 返回零

Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
本文介绍了Typeorm .loadRelationCountAndMap 返回零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

请帮忙.我正在尝试执行以下 typeorm 查询:

please help. I am trying to execute the following typeorm query:

  return await getRepository(Company)
    .createQueryBuilder("Company")
    .leftJoinAndSelect("Company.plants", "Plant")
    .leftJoinAndSelect("Plant.documents", "Document")
    .leftJoinAndSelect("Plant.notes", "Note")
    .loadRelationCountAndMap("Plant.documentsCount", "Plant.documents")
    .loadRelationCountAndMap("Plant.notesCount", "Plant.notes")
    .getMany();

我们的想法是选择每个工厂以及所有公司的所有工厂的文档和笔记数量.(实际上不需要自己选择笔记和文件,但我这样做是为了证明关系确实有效).

The idea was to select counts of documents and notes per each plant along with all plants for all companies. (Actually selecting notes and documents themselves was not needed, but i did it to prove that relations do work).

此外,我还指定了占位符变量以在 Plant 实体中保持计数:

Also I have specified the placeholder variables to keep counts in Plant entity:

  @OneToMany(() => Document, (document) => document.plant)
  documents: Document[];
  documentsCount: number;

  @OneToMany(() => Note, (note) => note.plant)
  notes: Note[];
  notesCount: number;

奇怪的是返回的Plant.documentsCount和Plant.notesCount都是0(而文档和笔记的集合不是空的,正在被选中).

Strangely the returned Plant.documentsCount and Plant.notesCount are 0 (while the collections of documents and notes are not empty and are being selected).

另一件奇怪的事情是,我在 SQL 查询中没有看到任何选择这些计数的尝试,因此我希望 typeorm 本身可以进行计数(因为它正确选择了集合).

Another strange thing is that i don't see in SQL querires any attempts to select these counts, thus i hope typeorm itself would do counting (since it has collections selected correctly).

有人可以就如何选择这些计数提供一些建议吗?

Could anybody please give some advise on how to select these counts?

推荐答案

不幸的是 Typeorm 是最无能的框架.所有重要功能都已弃用或未实现.

Unfortunately Typeorm is the most impotent framework. All important features are either deprecated or not implemented.

为了解决这个特定问题,我必须:

To solve this particular issue i had to:

  1. 选择集合本身:

   .leftJoinAndSelect("Plant.documents", "Document")
   .leftJoinAndSelect("Plant.notes", "Note")

  1. 添加计算和冗余关系数组删除:

  @AfterLoad()
  getDocumentsCount() {
    this.documentsCount = this.documents.length;
    delete this.documents;
  }

  @AfterLoad()
  getNotesCount() {
    this.notesCount = this.notes.length;
    delete this.notes;
  }

  1. 决定永远不再使用 TypeORM.

这篇关于Typeorm .loadRelationCountAndMap 返回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Query with t(n) and multiple cross joins(使用 t(n) 和多个交叉连接进行查询)
Unpacking a binary string with TSQL(使用 TSQL 解包二进制字符串)
Max rows in SQL table where PK is INT 32 when seed starts at max negative value?(当种子以最大负值开始时,SQL 表中的最大行数其中 PK 为 INT 32?)
Inner Join and Group By in SQL with out an aggregate function.(SQL 中的内部连接和分组依据,没有聚合函数.)
Add a default constraint to an existing field with values(向具有值的现有字段添加默认约束)
SQL remove from running total(SQL 从运行总数中删除)