[英]TypeORM findOne with nested relations
我在使用 TypeORM 执行嵌套查找查询时遇到一些问题。 这是基本代码:
const { completionId } = req?.params;
const user = req.user;
const retrievedCompletion = await getRepository(
CompletionGoogleSearch
).findOne({
relations: ['run', 'run.user'],
where: {
id: completionId,
// run: { user: { id: user.id } }, // This is the code that breaks the function
},
});
console.log(retrievedCompletion?.run.user.id);
console.log(user.id);
在我看来,一切正常,查询应该运行。 知道我做错了什么吗? 我知道我可以通过编写查询构建器查询或使用原始 SQL 来解决这个问题——我只是想知道我的代码是否存在缺陷。
typeorm 尚不支持您询问的功能(2021 年 2 月)。
查看 2018 年开放的这个问题。
解决方案是在run.user
实体中使用eager:true
:
@OneToOne(() => User, User=> User.run, {
eager:true
})
user: User;
下次您在CompletionGoogleSearch
中搜索时,只需执行relations: ['run']
并且用户会随之而来。
typeorm 添加了使用嵌套 object 的能力
userRepository.find({
relations: {
profile: true,
photos: true,
videos: {
videoAttributes: true,
},
},
})
这样,您可以在不使用 eager 的情况下获取数据。
你可以在这里找到更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.