首页 > 解决方案 > Postgres leftJoinAndMapMany 使用 TypeORM 查询(属性未定义)

问题描述

我一直在使用 TypeORM 使用 PostgreSQL,但在获取 Follower 表并检查用户是否在该表中时遇到了一些问题。

这是我的代码:

// queryTest.ts

data = await getRepository(User)
    .createQueryBuilder('user')
    .leftJoinAndMapMany('user.id', Post, 'post', 'post.userId = user.id')
    .leftJoinAndMapMany('post.id', Follow, 'follow', 'follow.userId = user.id')
    // Post author's follower
    .where(new Brackets(qb => {
      qb.where('post.publicScope = :follower', { follower: 'Follower' })
        .andWhere('post.userId = follow.followeeId')
        .andWhere(':myId = follow.userId', { myId: user.id })
    }))
    .getMany()

当我尝试发送这个查询时,我得到{ }(应该得到一些用户、帖子、关注者数据)

我有User, Post,Follow实体,我正在尝试获取这些数据:

我的追随者的帖子,他设置为“追随者”(假设我正在追随某个名人,他发布了仅针对追随者的内容,而我正在尝试获取该数据)

或者我也在考虑具有 ManyToMany 关系的 makig 2 个实体(Follower、Followee)。任何机构都可以帮我解决这个问题吗?

标签: postgresqltypeorm

解决方案


推荐阅读