Skip to content

Commit d490793

Browse files
authored
fix: left instead of inner join for where or + optional relations (#9516)
1 parent fc3b4f8 commit d490793

File tree

6 files changed

+327
-13
lines changed

6 files changed

+327
-13
lines changed

src/query-builder/SelectQueryBuilder.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -4267,16 +4267,13 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
42674267
)
42684268
if (!existJoin) {
42694269
this.joins.push({
4270-
type: "inner",
4270+
type: "left",
42714271
select: false,
42724272
selection: undefined,
42734273
alias: joinAlias,
42744274
parentAlias: alias,
42754275
relationMetadata: relation,
42764276
})
4277-
} else {
4278-
if (existJoin.type === "left")
4279-
existJoin.type = "inner"
42804277
}
42814278

42824279
const condition = this.buildWhere(

test/functional/find-options/basic-usage/find-options-order.ts

+106-5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ describe("find options > order", () => {
5353
text: "About post #3",
5454
counters: { likes: 1 },
5555
},
56+
{
57+
id: 4,
58+
title: "Post #4",
59+
text: "About post #4",
60+
counters: { likes: 1 },
61+
},
5662
])
5763

5864
const posts2 = await connection
@@ -82,6 +88,12 @@ describe("find options > order", () => {
8288
text: "About post #3",
8389
counters: { likes: 1 },
8490
},
91+
{
92+
id: 4,
93+
title: "Post #4",
94+
text: "About post #4",
95+
counters: { likes: 1 },
96+
},
8597
])
8698

8799
const posts3 = await connection
@@ -111,6 +123,12 @@ describe("find options > order", () => {
111123
text: "About post #3",
112124
counters: { likes: 1 },
113125
},
126+
{
127+
id: 4,
128+
title: "Post #4",
129+
text: "About post #4",
130+
counters: { likes: 1 },
131+
},
114132
])
115133

116134
const posts4 = await connection
@@ -142,6 +160,12 @@ describe("find options > order", () => {
142160
text: "About post #3",
143161
counters: { likes: 1 },
144162
},
163+
{
164+
id: 4,
165+
title: "Post #4",
166+
text: "About post #4",
167+
counters: { likes: 1 },
168+
},
145169
])
146170

147171
const posts5 = await connection
@@ -153,6 +177,12 @@ describe("find options > order", () => {
153177
})
154178
.getMany()
155179
posts5.should.be.eql([
180+
{
181+
id: 4,
182+
title: "Post #4",
183+
text: "About post #4",
184+
counters: { likes: 1 },
185+
},
156186
{
157187
id: 3,
158188
title: "Post #3",
@@ -182,6 +212,12 @@ describe("find options > order", () => {
182212
})
183213
.getMany()
184214
posts6.should.be.eql([
215+
{
216+
id: 4,
217+
title: "Post #4",
218+
text: "About post #4",
219+
counters: { likes: 1 },
220+
},
185221
{
186222
id: 3,
187223
title: "Post #3",
@@ -211,6 +247,12 @@ describe("find options > order", () => {
211247
})
212248
.getMany()
213249
posts7.should.be.eql([
250+
{
251+
id: 4,
252+
title: "Post #4",
253+
text: "About post #4",
254+
counters: { likes: 1 },
255+
},
214256
{
215257
id: 3,
216258
title: "Post #3",
@@ -242,6 +284,12 @@ describe("find options > order", () => {
242284
})
243285
.getMany()
244286
posts8.should.be.eql([
287+
{
288+
id: 4,
289+
title: "Post #4",
290+
text: "About post #4",
291+
counters: { likes: 1 },
292+
},
245293
{
246294
id: 3,
247295
title: "Post #3",
@@ -278,6 +326,12 @@ describe("find options > order", () => {
278326
})
279327
.getMany()
280328
posts.should.be.eql([
329+
{
330+
id: 4,
331+
title: "Post #4",
332+
text: "About post #4",
333+
counters: { likes: 1 },
334+
},
281335
{
282336
id: 3,
283337
title: "Post #3",
@@ -315,6 +369,12 @@ describe("find options > order", () => {
315369
})
316370
.getMany()
317371
posts.should.be.eql([
372+
{
373+
id: 4,
374+
title: "Post #4",
375+
text: "About post #4",
376+
counters: { likes: 1 },
377+
},
318378
{
319379
id: 3,
320380
title: "Post #3",
@@ -359,6 +419,12 @@ describe("find options > order", () => {
359419
text: "About post #3",
360420
counters: { likes: 1 },
361421
},
422+
{
423+
id: 4,
424+
title: "Post #4",
425+
text: "About post #4",
426+
counters: { likes: 1 },
427+
},
362428
{
363429
id: 1,
364430
title: "Post #1",
@@ -373,8 +439,8 @@ describe("find options > order", () => {
373439
},
374440
])
375441
expect(posts[0].id).to.be.eql(3)
376-
expect(posts[1].id).to.be.oneOf([1, 2])
377-
expect(posts[2].id).to.be.oneOf([1, 2])
442+
expect(posts[1].id).to.be.oneOf([1, 2, 4])
443+
expect(posts[2].id).to.be.oneOf([1, 2, 4])
378444
expect(posts[1].id).to.not.be.eql(posts[2].id)
379445
}),
380446
))
@@ -413,6 +479,12 @@ describe("find options > order", () => {
413479
text: "About post #2",
414480
counters: { likes: 2 },
415481
},
482+
{
483+
id: 4,
484+
title: "Post #4",
485+
text: "About post #4",
486+
counters: { likes: 1 },
487+
},
416488
])
417489
}),
418490
))
@@ -451,6 +523,12 @@ describe("find options > order", () => {
451523
text: "About post #2",
452524
counters: { likes: 2 },
453525
},
526+
{
527+
id: 4,
528+
title: "Post #4",
529+
text: "About post #4",
530+
counters: { likes: 1 },
531+
},
454532
])
455533
}),
456534
))
@@ -477,6 +555,12 @@ describe("find options > order", () => {
477555
.getMany()
478556
// exact row order depends of settings like NULLS FIRST and NULLS LAST
479557
posts.should.have.deep.members([
558+
{
559+
id: 4,
560+
title: "Post #4",
561+
text: "About post #4",
562+
counters: { likes: 1 },
563+
},
480564
{
481565
id: 1,
482566
title: "Post #1",
@@ -496,9 +580,10 @@ describe("find options > order", () => {
496580
counters: { likes: 1 },
497581
},
498582
])
499-
expect(posts[0].id).to.be.oneOf([1, 3])
583+
expect(posts[0].id).to.be.oneOf([4, 3])
500584
expect(posts[1].id).to.be.oneOf([2, 1])
501-
expect(posts[2].id).to.be.oneOf([3, 2])
585+
expect(posts[2].id).to.be.oneOf([2, 1])
586+
expect(posts[3].id).to.be.oneOf([3, 4])
502587
}),
503588
))
504589

@@ -537,6 +622,12 @@ describe("find options > order", () => {
537622
text: "About post #3",
538623
counters: { likes: 1 },
539624
},
625+
{
626+
id: 4,
627+
title: "Post #4",
628+
text: "About post #4",
629+
counters: { likes: 1 },
630+
},
540631
])
541632
}),
542633
))
@@ -561,7 +652,7 @@ describe("find options > order", () => {
561652
},
562653
})
563654
.getMany()
564-
posts.should.be.eql([
655+
posts.should.have.deep.members([
565656
{
566657
id: 3,
567658
title: "Post #3",
@@ -574,7 +665,17 @@ describe("find options > order", () => {
574665
text: "About post #1",
575666
counters: { likes: 1 },
576667
},
668+
{
669+
id: 4,
670+
title: "Post #4",
671+
text: "About post #4",
672+
counters: { likes: 1 },
673+
},
577674
])
675+
expect(posts[0].id).to.be.eql(3)
676+
expect(posts[1].id).to.be.oneOf([1, 4])
677+
expect(posts[2].id).to.be.oneOf([1, 4])
678+
expect(posts[1].id).to.not.be.eql(posts[2].id)
578679
}),
579680
))
580681
})

0 commit comments

Comments
 (0)