微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在typeorm中有关系时找不到软删除的行-nestJs

如何解决在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;

cortes 实体:

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

解决方法

我修复了一个新查询,在我上次查询中,{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}`);
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。