Search code examples
nestjstypeormmany-to-onesoft-delete

Can't find soft deleted rows when have relations in typeorm - nestJs


When I try to find a row that was soft deleted , even with {with_Deleted : true}, returns null , but when the row was not soft-deleted it returns normal. Is there a way that it can return soft deleted rows?

conjunto-simulacoes service :

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

conjunto-simulacoes Controller :

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

conjunto-simulacoes entity :

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

cortes entity :

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

Solution

  • I fixed doing a new query , in my last query the {with_Deleted : true} was searching inside the table conjunto simulacoes and not in the table cortes.

    New query:

        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}`);
        }