Skip to content

Commit

Permalink
fix: hangup when load relations with relationLoadStrategy: query (#10630
Browse files Browse the repository at this point in the history
)

* fix: createQueryBuilder to accept queryRunner
* fix: modify to use queryRunner when retrieving relations (#10481)
* test: add testcases
  • Loading branch information
casheeeewnuts committed Jan 26, 2024
1 parent dd49a25 commit 54d8d9e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/query-builder/QueryBuilder.ts
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
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
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
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

0 comments on commit 54d8d9e

Please sign in to comment.