[英]TypeORM - returns double result
我在 typeorm 中通过 querybuilder 进行搜索时遇到问题:
const projects = await this.conn.getRepository(UserProjectRelations).createQueryBuilder('userProject')
.innerJoin("userProject.userId", "user", "user.id = :userId", { userId: user.id })
.innerJoinAndSelect("userProject.projectdId", "projectdId")
.addSelect(['userProject.id',
'userProject.status',
])
.getRawMany();
为什么当我启动这个查询时,我得到一个像id
和userProject_id
这样的双行?
output 如下:
{
"userProject_id": 1,
"userProject_status": "Owner",
"status": "Owner",
"id": 1
}
我只想返回这两个对象:
{
"status": "Owner",
"id": 1
}
谢谢你的帮助!
我认为当您使用 getRawMany() 时,它会返回您提到的原始数据: userProject_id
和userProject_status
如果您尝试改用 getMay() 会怎样。 我认为如果你这样做,你会得到你想要的形状的 object。 仅是 {id, status}
const projects = await this.conn
.getRepository(UserProjectRelations)
.createQueryBuilder('userProject')
.innerJoin('userProject.userId', 'user', 'user.id = :userId', {
userId: user.id,
})
.innerJoinAndSelect('userProject.projectdId', 'projectdId')
.addSelect(['userProject.id', 'userProject.status'])
.getMany(); // Try this instead of getRawMany()
Get Raw 将返回原始数据并为您提供比您想要的更多的信息。 它适用于生成实体上不存在的字段,例如在其他字段值上使用 sum 或 avg。
以下是有关获取原始数据的一些进一步阅读: https://github.com/typeorm/typeorm/blob/master/docs/select-query-builder.md#getting-raw-results
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.