Skip to content

Commit

Permalink
fix: left instead of inner join for where or + optional relations (#9516
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pfgallagher committed Dec 3, 2022
1 parent fc3b4f8 commit d490793
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 13 deletions.
5 changes: 1 addition & 4 deletions src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4267,16 +4267,13 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
)
if (!existJoin) {
this.joins.push({
type: "inner",
type: "left",
select: false,
selection: undefined,
alias: joinAlias,
parentAlias: alias,
relationMetadata: relation,
})
} else {
if (existJoin.type === "left")
existJoin.type = "inner"
}

const condition = this.buildWhere(
Expand Down
111 changes: 106 additions & 5 deletions test/functional/find-options/basic-usage/find-options-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ describe("find options > order", () => {
text: "About post #3",
counters: { likes: 1 },
},
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
])

const posts2 = await connection
Expand Down Expand Up @@ -82,6 +88,12 @@ describe("find options > order", () => {
text: "About post #3",
counters: { likes: 1 },
},
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
])

const posts3 = await connection
Expand Down Expand Up @@ -111,6 +123,12 @@ describe("find options > order", () => {
text: "About post #3",
counters: { likes: 1 },
},
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
])

const posts4 = await connection
Expand Down Expand Up @@ -142,6 +160,12 @@ describe("find options > order", () => {
text: "About post #3",
counters: { likes: 1 },
},
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
])

const posts5 = await connection
Expand All @@ -153,6 +177,12 @@ describe("find options > order", () => {
})
.getMany()
posts5.should.be.eql([
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
{
id: 3,
title: "Post #3",
Expand Down Expand Up @@ -182,6 +212,12 @@ describe("find options > order", () => {
})
.getMany()
posts6.should.be.eql([
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
{
id: 3,
title: "Post #3",
Expand Down Expand Up @@ -211,6 +247,12 @@ describe("find options > order", () => {
})
.getMany()
posts7.should.be.eql([
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
{
id: 3,
title: "Post #3",
Expand Down Expand Up @@ -242,6 +284,12 @@ describe("find options > order", () => {
})
.getMany()
posts8.should.be.eql([
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
{
id: 3,
title: "Post #3",
Expand Down Expand Up @@ -278,6 +326,12 @@ describe("find options > order", () => {
})
.getMany()
posts.should.be.eql([
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
{
id: 3,
title: "Post #3",
Expand Down Expand Up @@ -315,6 +369,12 @@ describe("find options > order", () => {
})
.getMany()
posts.should.be.eql([
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
{
id: 3,
title: "Post #3",
Expand Down Expand Up @@ -359,6 +419,12 @@ describe("find options > order", () => {
text: "About post #3",
counters: { likes: 1 },
},
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
{
id: 1,
title: "Post #1",
Expand All @@ -373,8 +439,8 @@ describe("find options > order", () => {
},
])
expect(posts[0].id).to.be.eql(3)
expect(posts[1].id).to.be.oneOf([1, 2])
expect(posts[2].id).to.be.oneOf([1, 2])
expect(posts[1].id).to.be.oneOf([1, 2, 4])
expect(posts[2].id).to.be.oneOf([1, 2, 4])
expect(posts[1].id).to.not.be.eql(posts[2].id)
}),
))
Expand Down Expand Up @@ -413,6 +479,12 @@ describe("find options > order", () => {
text: "About post #2",
counters: { likes: 2 },
},
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
])
}),
))
Expand Down Expand Up @@ -451,6 +523,12 @@ describe("find options > order", () => {
text: "About post #2",
counters: { likes: 2 },
},
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
])
}),
))
Expand All @@ -477,6 +555,12 @@ describe("find options > order", () => {
.getMany()
// exact row order depends of settings like NULLS FIRST and NULLS LAST
posts.should.have.deep.members([
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
{
id: 1,
title: "Post #1",
Expand All @@ -496,9 +580,10 @@ describe("find options > order", () => {
counters: { likes: 1 },
},
])
expect(posts[0].id).to.be.oneOf([1, 3])
expect(posts[0].id).to.be.oneOf([4, 3])
expect(posts[1].id).to.be.oneOf([2, 1])
expect(posts[2].id).to.be.oneOf([3, 2])
expect(posts[2].id).to.be.oneOf([2, 1])
expect(posts[3].id).to.be.oneOf([3, 4])
}),
))

Expand Down Expand Up @@ -537,6 +622,12 @@ describe("find options > order", () => {
text: "About post #3",
counters: { likes: 1 },
},
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
])
}),
))
Expand All @@ -561,7 +652,7 @@ describe("find options > order", () => {
},
})
.getMany()
posts.should.be.eql([
posts.should.have.deep.members([
{
id: 3,
title: "Post #3",
Expand All @@ -574,7 +665,17 @@ describe("find options > order", () => {
text: "About post #1",
counters: { likes: 1 },
},
{
id: 4,
title: "Post #4",
text: "About post #4",
counters: { likes: 1 },
},
])
expect(posts[0].id).to.be.eql(3)
expect(posts[1].id).to.be.oneOf([1, 4])
expect(posts[2].id).to.be.oneOf([1, 4])
expect(posts[1].id).to.not.be.eql(posts[2].id)
}),
))
})

0 comments on commit d490793

Please sign in to comment.