首页 > 解决方案 > 在 typeorm 中有关系时找不到软删除的行-nestJs

问题描述

当我试图找到一个被软删除的行时,即使有,也会{with_Deleted : true}返回 null ,但是当该行没有被软删除时,它会返回正常。有没有办法可以返回软删除的行?

conjunto-simulacoes 服务:

async getCorteById(id : number): Promise<ConjuntoSimulacoes>{
        return await this.conjuntoSimulacoesRepository.findOne({withDeleted : true,relations : ['corte'],where : {id}});
    }

conjunto-simulacoes 控制器:

    @Get('/corte/:id')
    @UseGuards(AuthGuard('jwt'))
    async getCorteBySimulacao(@Param('id') id : number){
        return await this.conjuntoSimulacoesService.getCorteById(id);
    }

conjunto-simulacoes 实体:

@ManyToOne(() => Cortes , corte => corte.conjunto_simulacoes )
    corte : Cortes;

科尔特斯实体:

@OneToMany(() => ConjuntoSimulacoes , conjunto_simulacoes => conjunto_simulacoes.corte )
    conjunto_simulacoes : ConjuntoSimulacoes[]

标签: nestjstypeormmany-to-onesoft-delete

解决方案


我修复了做一个新查询,在我的最后一个查询中{with_Deleted : true}是在表内conjunto simulacoes而不是在表中搜索cortes

新查询:

    async getCorteByIdWithDeleted(id : number){
        return await this.conjuntoSimulacoesRepository.query(`SELECT * FROM conjunto_simulacoes 
        as conjunto LEFT JOIN cortes as corte on corte.id = conjunto."corteId" 
        WHERE conjunto.id=${id}`);
    }

推荐阅读