gpt4 book ai didi

TypeORM:使用关系和位置进行选择请求

转载 作者:行者123 更新时间:2023-12-05 07:26:47 25 4
gpt4 key购买 nike

我用 NestJS 和 ORM TypeORM 做一个 API

我有实体:

用户

关系类型

UserRelationshipType(带有用户和关系类型 ForeignKey)

和关系(与 userRelationshipType 外键)

并且需要找到所有的关系,但是来自特定的用户所以我需要在某个地方声明“user.id = id”

我已经用 createQueryBuilder 做到了

this.repo
.createQueryBuilder('relationship')
.innerJoinAndSelect(
'relationship.userRelationshipType',
'userRelationshipType',
)
.innerJoinAndSelect(
'userRelationshipType.user',
'user',
'user.id = :id',
{ id: id },
)
.innerJoinAndSelect(
'userRelationshipType.relationshipType',
'relationshipType',
)
.getMany()

这是工作但我不高兴,因为我想用 .find 或 findOne 完成我的所有请求

当我在做的时候

this.repo.find({

relations: [

'userRelationshipType',

'userRelationshipType.user',

'userRelationshipType.relationshipType',

],

where: {userRelationshipType: {user: {id: id}}}

});

那是行不通的……但是在文档中说要这样做……我可以找到所有,但可以应用良好的 where 条件(或连接条件)你有想法吗?

我也试过加入,但我有点迷路

谢谢:)

最佳答案

如果你使用类验证器,那么你可以很容易地做到这一点..

--> 提供两个表之间的关系。
--> 为你不想在模型中选择的字段提供@exclude。
--> 不要忘记提及导入类验证程序包。
谢谢。


这里给你举个例子

 @Exclude()
@Column({ name: 'password' })
public password: string;

关于TypeORM:使用关系和位置进行选择请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54276900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com