如何使用查询生成器过滤一对多关系

时间:2019-09-11 11:38:47

标签: node.js query-builder typeorm

我有

@Entity('procedures')
export class ProcedureEntity {

    @PrimaryGeneratedColumn()
    id: number;

    @OneToMany(type => MediaFileEntity, media => media.procedure, {
        cascade: true
    })
    files: MediaFileEntity[];

};

@Entity('media_files')
export class MediaFileEntity {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({ nullable: true })
    deletedAt: Date;

    @ManyToOne(type => ProcedureEntity, procedure => procedure.files)
    procedure: ProcedureEntity;

};

有人知道我如何创建查询生成器,以便获得所有过程,但媒体文件具有deletedAt NULL吗?

我正在尝试

const procedures = await this.procedureRepository
    .createQueryBuilder("procedure")
    .leftJoinAndSelect("procedure.doctor", "doctor")
    .leftJoinAndSelect("procedure.patient", "patient")
    .innerJoin('procedure.files', 'mediaFile', 'mediaFile.deletedAt IS NULL')
    .where("(patient.id = :id OR doctor.id = :doctorId)", { id: user.id, doctorId: user.id })
    .andWhere('procedure.deletedAt IS NULL')
    .getMany();

但是我得到Cannot find name 'where'。这很奇怪,因为this.procedureRepository .createQueryBuilder("procedure")也是SelectQueryBuilder<ProcedureEntity>this.procedureRepository .createQueryBuilder("procedure") .leftJoinAndSelect("procedure.doctor", "doctor") .leftJoinAndSelect("procedure.patient", "patient") .innerJoin('procedure.files', 'mediaFile', 'mediaFile.deletedAt IS NULL');

谢谢!

编辑:有趣的是,我已经有了答案。但是我还是要把它留在这里,因为后代ity

1 个答案:

答案 0 :(得分:0)

您遇到语法错误。 innerJoin之后是分号。这是错误的原因吗?