关系中的Typeorm OneToMany过滤器不影响结果 [英] Typeorm OneToMany filter in relations not efftect to result

查看:463
本文介绍了关系中的Typeorm 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;
}

当我尝试对类别进行过滤时,结果没有过滤

When I try the filter on 'categories' but the result doesn't filter 'categories' follow the key that I already a push.

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


推荐答案

我们需要使用 queryBuilder 对于这样的情况,因为查找不允许过滤关系:

We need to use queryBuilder for cases like this since find doesn't allow filtering relations:

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

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

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