Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: left instead of inner join for where or + optional relations #9516

Merged
merged 1 commit into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: left instead of inner join for where or + optional relations
  • Loading branch information
pfgallagher committed Nov 2, 2022
commit ee4d27c6befee4f9999ded902a3ea4caeca8b5b3
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)
}),
))
})
Loading