【问题标题】:TypeORM: Make a select request with relation and whereTypeORM:使用关系和位置发出选择请求
【发布时间】:2019-06-14 01:43:18
【问题描述】:

我用 NestJS 和 ORM TypeORM 做一个 API

我有实体:

用户

关系类型

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

和关系(使用 userRelationshipType ForeignKey)

并且需要查找所有关系,但来自特定用户 所以我需要在某个地方声明“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 条件(或 join 条件) 你有什么想法吗?

我也尝试过加入,但我有点迷茫

谢谢:)

【问题讨论】:

标签: typeorm


【解决方案1】:

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

--> 提供两个表之间的关系。
--> 为您不想在模型中选择的字段提供@exclude。
--> 别忘了提到导入类验证器包。
谢谢。


这里的例子给你
 @Exclude()
    @Column({ name: 'password' })
    public password: string;

【讨论】:

    猜你喜欢
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 2022-07-12
    • 2021-07-06
    • 1970-01-01
    • 2022-07-16
    • 2020-08-01
    相关资源
    最近更新 更多