【问题标题】:TypeORM second JOIN statement does not provide resultsTypeORM第二条JOIN语句不提供结果
【发布时间】:2021-12-08 12:22:29
【问题描述】:

我有一个包含产品、制造商和收藏品的数据库。产品和制造商之间存在 n:m 关系。在集合中,用户可以存储产品,因此产品和集合也以 n:m 关系连接。

现在我必须根据 collectionId 向制造商加载产品。

目前我有以下sn-p:

CollectionProducts.createQueryBuilder("cp")
    .innerJoinAndSelect("cp.product", "product")
    .leftJoinAndSelect(
      Manufacturer,
      "manufacturer",
      "product.manufacturerId = manufacturer.id"
    )
    .where("cp.collectionId IN(:...ids)", { ids: collectionIds })
    .getMany();

产品加载成功,但没有加载厂家。

这是打印出来的结果:

[
  CollectionProducts {
    collectionId: 1,
    productId: 1,
    __product__: Product {
      id: 1,
      name: 'Tea',
      productImage: '',
      description: 'Peach',
      releaseDate: 2021-10-19T18:13:42.938Z,
      status: null,
      origin: 'Germany',
      manufacturerId: 1
    }
  }
]

有人知道我在哪里做错了吗?

【问题讨论】:

    标签: node.js database typescript postgresql typeorm


    【解决方案1】:

    我已经为我的具体问题找到了解决方案。我不得不使用getRawMany(),而不是使用getMany()。生成的 SQL 本身是正确的,getRawMany() 我得到了所有需要的结果。

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2012-05-19
      • 1970-01-01
      • 2015-10-27
      • 2021-08-04
      • 2014-09-18
      • 1970-01-01
      • 2013-12-24
      相关资源
      最近更新 更多