Repository navigation
perf(relations)!: use inner join for non-nullable ManyToOne and OneToOne relations - #12064
Conversation
Like JPA's @joincolumn(nullable = false) behavior, use INNER JOIN when a relation is explicitly marked as nullable: false. This is semantically correct since the related entity is guaranteed to exist, and allows the DB optimizer to produce more efficient query plans. Relations with nullable: true (the default) continue using LEFT JOIN, preserving backward compatibility.
Review Summary by QodoUse INNER JOIN for nullable=false relations instead of LEFT JOIN
WalkthroughsDescription• Use INNER JOIN instead of LEFT JOIN for nullable: false relations • Applied across SelectQueryBuilder.ts (4 locations) and FindOptionsUtils.ts (3 locations) • Duplicate join detection updated to accept both LEFT and INNER joins • Added test entity field and test case verifying INNER JOIN for non-nullable eager relations Diagramflowchart LR
A["Relation metadata"]
B["relation.isNullable = true"]
C["relation.isNullable = false"]
D["LEFT JOIN"]
E["INNER JOIN"]
A -- "check nullability" --> B
A -- "check nullability" --> C
B -- "generates" --> D
C -- "generates" --> E
File Changes1. src/find-options/FindOptionsUtils.ts
|
Code Review by Qodo
1.
|
commit: |
|
Persistent review updated to latest commit 8967fa4 |
|
Persistent review updated to latest commit 9e3ffd1 |
|
Thanks for the PR — the concept is solid. I've made a few amendments and pushed them to the branch: 1. Scoped INNER JOIN to join-column relations only The original code used Changed all 6 locations to: 2. Strengthened test assertions The original test only checked expect(query).to.match(/INNER JOIN .?user.? .?post__requiredAuthor.?/)
expect(query).to.match(/LEFT JOIN .?user.? .?post__author.?/)3. Added migration guide entry Documented the behavior change under |
|
Persistent review updated to latest commit d366eca |
|
Follow-up to my previous review — pushed additional changes: 4. Soft-delete safety If the target entity has a Fixed: when the target entity has 5. Comprehensive join strategy test suite Added Each test verifies behavior through result counts — two posts are created (one with all relations, one with only required ones), so LEFT JOIN and INNER JOIN produce observably different results. QueryBuilder tests additionally assert the SQL join type via regex. 6. Relations documentation updated Updated 7. SonarQube fix Flattened nested |
|
Persistent review updated to latest commit 2c729e9 |
|
Persistent review updated to latest commit 448d941 |
|
Persistent review updated to latest commit 1809cc6 |
|
|
Persistent review updated to latest commit c597ecd |
…One relations (#12064) Co-authored-by: Piotr Kuczynski <[email protected]>



Summary
INNER JOINinstead ofLEFT JOINwhen a relation is explicitly marked asnullable: false, matching JPA's@JoinColumn(nullable = false)behaviornullable: true(the default) continue usingLEFT JOIN, preserving full backward compatibilityChanges
SelectQueryBuilder.ts(4 locations):type: "left"→type: relation.isNullable ? "left" : "inner"inbuildRelations(),buildEagerRelations(),buildOrder(),buildWhere()FindOptionsUtils.ts(3 locations): conditionalleftJoin/innerJoinandleftJoinAndSelect/innerJoinAndSelectbased onrelation.isNullable, plus duplicate join validation fix for INNER joinsnullable: falseeager relation entity and test case verifying INNER JOIN generationTest plan
INNER JOINfornullable: falserelations