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: Hangup when load relations with relationLoadStrategy: query #10630

Merged
merged 3 commits into from
Jan 26, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/query-builder/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,8 @@ export abstract class QueryBuilder<Entity extends ObjectLiteral> {
* Creates a completely new query builder.
* Uses same query runner as current QueryBuilder.
*/
createQueryBuilder(): this {
return new (this.constructor as any)(this.connection, this.queryRunner)
createQueryBuilder(queryRunner?: QueryRunner): this {
return new (this.constructor as any)(this.connection, queryRunner ?? this.queryRunner)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3603,7 +3603,7 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
)
: this.findOptions.relations

const queryBuilder = this.createQueryBuilder()
const queryBuilder = this.createQueryBuilder(queryRunner)
.select(relationAlias)
.from(relationTarget, relationAlias)
.setFindOptions({
Expand Down
41 changes: 41 additions & 0 deletions test/integration/sample2-one-to-one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,47 @@ describe("one-to-one", function () {
.getOne()
.should.eventually.eql(expectedDetails)
})

it("should load post and its details with no hangup if query used", async function () {
if (!dataSource) return
const expectedPost = new Post()
expectedPost.id = savedPost.id
expectedPost.text = savedPost.text
expectedPost.title = savedPost.title
expectedPost.details = new PostDetails()
expectedPost.details!.id = savedPost.details!.id
expectedPost.details!.authorName = savedPost.details!.authorName
expectedPost.details!.comment = savedPost.details!.comment
expectedPost.details!.metadata = savedPost.details!.metadata

const findOne = () => postRepository.findOne({
where: {
id: savedPost.id
},
relations: {
details: true
},
relationLoadStrategy: "query"
})

const posts = await Promise.all([
findOne(),
findOne(),
findOne(),
findOne(),
findOne(),
findOne(),
findOne(),
findOne(),
findOne(),
findOne(),
])

posts.forEach(post => {
expect(post).not.to.be.null
post!.should.eql(expectedPost)
})
})
})

describe("insert post and category (one-side relation)", function () {
Expand Down
41 changes: 41 additions & 0 deletions test/integration/sample3-many-to-one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,47 @@ describe("many-to-one", function () {
.getOne()
.should.eventually.eql(expectedDetails)
})

it("should load post and its details with no hangup if query used", async function () {
if (!dataSource) return
const expectedPost = new Post()
expectedPost.id = savedPost.id
expectedPost.text = savedPost.text
expectedPost.title = savedPost.title
expectedPost.details = new PostDetails()
expectedPost.details!.id = savedPost.details!.id
expectedPost.details!.authorName = savedPost.details!.authorName
expectedPost.details!.comment = savedPost.details!.comment
expectedPost.details!.metadata = savedPost.details!.metadata

const findOne = () => postRepository.findOne({
where: {
id: savedPost.id
},
relations: {
details: true
},
relationLoadStrategy: "query"
})

const posts = await Promise.all([
findOne(),
findOne(),
findOne(),
findOne(),
findOne(),
findOne(),
findOne(),
findOne(),
findOne(),
findOne(),
])

posts.forEach(post => {
expect(post).not.to.be.null
post!.should.eql(expectedPost)
})
})
})

describe("insert post and category (one-side relation)", function () {
Expand Down