首页 文章

查询后未显示TypeORM选中的子查询结果

提问于
浏览
0

所以我有以下查询:

const [discussions, total] = await em
    .createQueryBuilder(Discussion, 'd')
    .select(['d', 'u.id', 'u.firstName', 'u.lastName', 'u.image'])
    .addSelect(
      qb =>
        qb
          .select('COUNT(*)', 'commentCount')
          .from(Comment, 'c')
          .where('c.discussion_id = d.id'),
      'commentCount',
    )
    .leftJoin('d.user', 'u', 'd.user_id = u.id')
    .where('d.project_id = :projectId', { projectId })
    .orderBy({ 'd.updated_date': 'DESC' })
    .limit(limit)
    .offset(offset)
    .getManyAndCount();

我正在添加子查询选择,如您所见 .

查询执行成功,但 discussions 数组没有返回属性 commentCount .

我错过了什么?

1 回答

  • 1

    使用getRaw或getRawMany而不是getManyAndCount . 当您选择字段时,typeorm无法将其映射到您已定义的实体,因为select中可能存在别名 . 所以你必须将这些数据作为原始数据 .

相关问题