关系中的类型ORM OneToMany筛选器对结果没有影响 [英] TypeORM OneToMany filter in relations not effect to result

查看:28
本文介绍了关系中的类型ORM OneToMany筛选器对结果没有影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表:

@Entity('Reviews')
class Review {
  ...
  @OneToMany((type) => MapCategory, map => map.review)
  public categories: MapCategory[];
}

和:

@Entity('MapCategories')
export class MapCategory {
  ...
  @ManyToOne(type => Review, (review) => review.categories)
  public review: Review;
}

当我在'categories'上尝试筛选,但结果没有按照我已经按下的键筛选'categories'时。

const items = await this.reviewRepository.findAndCount({
  relations: ['categories'],
  where: {
    categories: {
      id: 1
    }
  }
});

推荐答案

对于这种情况,我们需要使用queryBuilder,因为find不允许筛选关系:

const items = await reviewRepository.createQueryBuilder("review")
    .leftJoinAndSelect("review.categories", "category")
    .where("category.id = :id", { id })
    .getManyAndCount()

这篇关于关系中的类型ORM OneToMany筛选器对结果没有影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆