Repository navigation
Conversation
…specified When an eager relation is also explicitly specified in the `relations` option of a find query, the relation was joined twice with different aliases. This happened because the dedup check in `joinEagerRelations` rejected existing joins that had a condition set (e.g., from DeleteDateColumn), causing it to add a duplicate join. Removed the `join.condition !== undefined` check from the dedup logic in `joinEagerRelations`, since conditions added automatically by TypeORM (like DeleteDateColumn soft-delete filters) should not prevent dedup matching. Fixes typeorm#11823
User descriptionProblemWhen an eager relation is also explicitly specified in the For example, given these entities: // Editor has an eager OneToOne relation to User
@OneToOne(() => User, { eager: true })
@JoinColumn()
user: User
// User has a DeleteDateColumn and an eager OneToOne to Profile
@DeleteDateColumn()
deletedAt?: Date
@OneToOne(() => Profile, { eager: true })
@JoinColumn()
profile: ProfileRunning this query: connection.manager.findOne(Editor, {
where: { userId: 1 },
relations: { user: { nestedProfile: true } },
})Would generate SQL with duplicate joins for the Root CauseIn However, when the joined entity has a FixRemoved the TestAdded a test that inspects the generated SQL to verify the user table is only joined once when an eager relation with Fixes #11823 PR TypeBug fix Description
Diagram Walkthroughflowchart LR
A["Eager relation + explicit relation specification"] -->|Previously| B["Duplicate joins with different aliases"]
A -->|After fix| C["Single join with correct alias"]
D["DeleteDateColumn adds condition to join"] -->|Removed check| E["Condition no longer blocks dedup matching"]
|
| Relevant files | |||
|---|---|---|---|
| Bug fix |
| ||
| Tests |
|
PR Code Suggestions ✨Latest suggestions up to 87549e2
Previous suggestionsSuggestions up to commit dbc2122
Suggestions up to commit a2c83e1
Suggestions up to commit 34e9c35
Suggestions up to commit 7a0c5db
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
Code Review by Qodo
1. Empty @PARAM JSDoc tags
|
commit: |
- Remove description-less @PARAM annotations that add noise without value - Add typeorm#11823 issue reference to the test name for traceability Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Thanks @gioboa! Happy to help. Let me know if there's anything else you'd like me to adjust. |
|
Persistent review updated to latest commit 34e9c35 |
|
Hi team, friendly bump on this PR — it fixes duplicate JOINs when an eager relation is also explicitly listed in the relations option. The root cause was a condition check in joinEagerRelations that rejected valid dedup matches. Tests are passing. Would appreciate a review when someone has a chance! |
|
Persistent review updated to latest commit a2c83e1 |
|
Persistent review updated to latest commit dbc2122 |
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewⓘ The new review experience is currently in Beta. Learn more |
Problem
When an eager relation is also explicitly specified in the
relationsoption of a find query, the same relation table gets joined twice with different aliases. This results in redundant SQL JOINs, which hurts query performance and produces unnecessarily large result sets.For example, given these entities:
Running this query:
Would generate SQL with duplicate joins for the
userandprofiletables (once from the explicit relations processing, once from the eager relations processing).Root Cause
In
FindOptionsUtils.joinEagerRelations(), the dedup check that detects if a relation has already been joined included a conditionjoin.condition !== undefined. This check rejected any existing join that had a condition set on it.However, when the joined entity has a
@DeleteDateColumn, TypeORM automatically adds a"alias"."deletedAt" IS NULLcondition to the join. This meant the dedup check would never match joins for entities with soft-delete columns, causing the eager relation to be added again with a different alias.Fix
Removed the
join.condition !== undefinedcheck from the dedup logic. The remaining checks (mapToProperty,isMappingMany,direction, andentityOrProperty) are sufficient to correctly identify duplicate joins. The condition on the join is an implementation detail added by TypeORM's join processing and shouldn't affect whether two joins are for the same relation.Test
Added a test that inspects the generated SQL to verify the user table is only joined once when an eager relation with
@DeleteDateColumnis explicitly specified inrelations.Fixes #11823