typeorm / typeorm/typeorm

QueryBuilder.withDeleted() position affects joined relations' soft delete filter

Open
#11,906 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug discussion
Dominant language
TypeScript
Stars
36.7k
Forks
6.7k
Avg merge
3d 1h
Merged PRs (30d)
6

Description

Issue description

When using QueryBuilder with .withDeleted() and join operations, the call position determines whether soft delete filters are applied to joined relations.

Expected Behavior

.withDeleted() should consistently include all soft-deleted records regardless of when it's called in the builder chain.

Actual Behavior
  • withDeleted() BEFORE joins: All entities' soft delete filters removed ✅
  • withDeleted() AFTER joins: Main entity's filter removed, but joined relations still filtered ❌
Steps to reproduce

Test Case 1: .withDeleted() AFTER joins (❌ INCORRECT)

  const recipients = await em
    .createQueryBuilder(Recipient, 'recipient')
    .leftJoinAndSelect('recipient.deal', 'deal')
    .leftJoinAndSelect('deal.contact', 'contact')
    .leftJoinAndSelect('deal.company', 'company')
    .where('recipient.team_id = :team_id', { team_id: 1 })
    .withDeleted()  // ← Position: AFTER joins
    .getMany();

Generated SQL (INCORRECT):

  // LEFT JOIN "deal" ON ... AND "deal"."deleted_at" IS NULL  ❌
  // LEFT JOIN "contact" ON ... AND "contact"."deleted_at" IS NULL  ❌
  // LEFT JOIN "company" ON ... AND "company"."deleted_at" IS NULL  ❌

Test Case 2: .withDeleted() BEFORE joins (✅ CORRECT)

  const recipients = await em
    .createQueryBuilder(Recipient, 'recipient')
    .withDeleted()  // ← Position: BEFORE joins
    .leftJoinAndSelect('recipient.deal', 'deal')
    .leftJoinAndSelect('deal.contact', 'contact')
    .leftJoinAndSelect('deal.company', 'company')
    .where('recipient.team_id = :team_id', { team_id: 1 })
    .getMany();

Generated SQL (CORRECT):

  // LEFT JOIN "deal" ON ...  ✅
  // LEFT JOIN "contact" ON ...  ✅
  // LEFT JOIN "company" ON ...  ✅
My Environment
Dependency Version
Operating System macOS
Node.js version 22.14.0
Typescript version 5.7.3
TypeORM version 0.3.21
Additional Context

No response

Relevant Database Driver(s)
  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver
Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, and I know how to start.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two createQueryBuilder chains using withDeleted(), leftJoinAndSelect(), and PostgreSQL, then compare the generated SQL for joined soft-delete filters. Done means withDeleted() removes the joined relations' soft-delete filters consistently regardless of whether it appears before or after the joins.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgres, typescript
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.