Fra*_*o S 20 javascript postgresql node.js typeorm
我在使用 TypeORM 执行嵌套查找查询时遇到一些问题。这是基本代码:
\n const { completionId } = req?.params;\n const user = req.user;\n\n const retrievedCompletion = await getRepository(\n CompletionGoogleSearch\n ).findOne({\n relations: [\'run\', \'run.user\'],\n where: {\n id: completionId,\n // run: { user: { id: user.id } }, // This is the code that breaks the function\n },\n });\n\n console.log(retrievedCompletion?.run.user.id);\n console.log(user.id);\nRun Code Online (Sandbox Code Playgroud)\n在我看来,没有任何问题,并且查询应该运行。知道我做错了什么吗?我知道我可以通过编写查询构建器查询或使用原始 SQL\xe2\x80\x93 来解决这个问题,我只是想知道我的代码中是否存在缺陷。
\n小智 19
typeorm 添加了使用嵌套对象的能力
userRepository.find({
relations: {
profile: true,
photos: true,
videos: {
videoAttributes: true,
},
},
});
Run Code Online (Sandbox Code Playgroud)
这样,你就可以在不使用 eager 的情况下获取数据。
您可以在这里找到更多信息
小智 6
eager:true解决方案在实体中使用run.user:
@OneToOne(() => User, User=> User.run, {
eager:true
})
user: User;
Run Code Online (Sandbox Code Playgroud)
下次您搜索CompletionGoogleSearchdo just时relations: ['run'],用户就会随之而来。
| 归档时间: |
|
| 查看次数: |
24235 次 |
| 最近记录: |