Postgresql Postgres/TypeForm-多个连接。我应该如何格式化查询?
我正在构建一个后端微服务,它使用postgres作为数据库和TypeORM(NodeJs)库来管理与它的连接 假设我的数据库包含3个表/实体Postgresql Postgres/TypeForm-多个连接。我应该如何格式化查询?,postgresql,typeorm,Postgresql,Typeorm,我正在构建一个后端微服务,它使用postgres作为数据库和TypeORM(NodeJs)库来管理与它的连接 假设我的数据库包含3个表/实体 用户表(主列为-id) 配置文件表(主列id,然后是指向用户表id列的外键用户id) 可能有多个配置文件行链接到一个用户 经验表(主列id,然后外键profile\u id列指向profile表id列) 可能有多个经验行链接到一个配置文件 a) 我是否可以执行一个复杂的查询来获取配置文件行和链接到它的所有经验行 getFullProfile(profi
getFullProfile(profileId: number) {
return this.profileEntityRepository.createQueryBuilder('profile')
.where('profile.id = :profileId', { profileId })
.innerJoinAndMapMany('profile.experience', ExperienceEntity, 'experience', 'experience.id = profile.id')
.getOne();
}
遗憾的是,这不会返回任何结果(如果我注释掉innerJoinAndMapMany行,就会返回)
有什么建议吗?它不必是基于类型化api的答案,简单的简单查询就足够了
谢谢 好的,我解决了
我将TypeForm查询更改为:
getFullProfile(profileId: number) {
return this.profileEntityRepository.createQueryBuilder('profile')
.leftJoinAndSelect('profile.experiences', 'experience')
.where('profile.id = :profileId', { profileId })
.getOne();
}
为了实现这一点,我还需要在profile entity中添加一列,如下所示:
@OneToMany(type => ExperienceEntity, experience => experience.profile)
experiences: ExperienceEntity[];
这不会在表中创建“经验”列,而是TypeORM需要它来了解情况