Skip to content

Commit 874e573

Browse files
grkov90Gregory Komagurov
andauthored
fix: Query builder makes query with joins, without limit for inherited entities (#6402)
Closes: #6399 Co-authored-by: Gregory Komagurov <Komagurov.GV@gazprom-neft.ru>
1 parent afc62fd commit 874e573

File tree

3 files changed

+118
-9
lines changed

3 files changed

+118
-9
lines changed

src/query-builder/QueryBuilder.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,10 @@ export abstract class QueryBuilder<Entity> {
598598
* Creates "WHERE" expression.
599599
*/
600600
protected createWhereExpression() {
601-
let conditions = this.createWhereExpressionString();
601+
const conditionsArray = [];
602+
603+
const whereExpression = this.createWhereExpressionString();
604+
whereExpression.trim() && conditionsArray.push(this.createWhereExpressionString());
602605

603606
if (this.expressionMap.mainAlias!.hasMetadata) {
604607
const metadata = this.expressionMap.mainAlias!.metadata;
@@ -609,7 +612,7 @@ export abstract class QueryBuilder<Entity> {
609612
: metadata.deleteDateColumn.propertyName;
610613

611614
const condition = `${this.replacePropertyNames(column)} IS NULL`;
612-
conditions = `${ conditions.length ? "(" + conditions + ") AND" : "" } ${condition}`;
615+
conditionsArray.push(condition);
613616
}
614617

615618
if (metadata.discriminatorColumn && metadata.parentEntityMetadata) {
@@ -618,17 +621,22 @@ export abstract class QueryBuilder<Entity> {
618621
: metadata.discriminatorColumn.databaseName;
619622

620623
const condition = `${this.replacePropertyNames(column)} IN (:...discriminatorColumnValues)`;
621-
return ` WHERE ${ conditions.length ? "(" + conditions + ") AND" : "" } ${condition}`;
624+
conditionsArray.push(condition);
622625
}
623626
}
624627

625-
if (!conditions.length) // TODO copy in to discriminator condition
626-
return this.expressionMap.extraAppendedAndWhereCondition ? " WHERE " + this.replacePropertyNames(this.expressionMap.extraAppendedAndWhereCondition) : "";
627-
628-
if (this.expressionMap.extraAppendedAndWhereCondition)
629-
return " WHERE (" + conditions + ") AND " + this.replacePropertyNames(this.expressionMap.extraAppendedAndWhereCondition);
628+
if (this.expressionMap.extraAppendedAndWhereCondition) {
629+
const condition = this.replacePropertyNames(this.expressionMap.extraAppendedAndWhereCondition);
630+
conditionsArray.push(condition);
631+
}
630632

631-
return " WHERE " + conditions;
633+
if (!conditionsArray.length) {
634+
return " ";
635+
} else if (conditionsArray.length === 1) {
636+
return ` WHERE ${conditionsArray[0]}`;
637+
} else {
638+
return ` WHERE ( ${conditionsArray.join(" ) AND ( ")} )`;
639+
}
632640
}
633641

634642
/**

test/github-issues/6399/entities.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {Entity, OneToMany, ManyToOne} from "../../../src";
2+
import {Column} from "../../../src";
3+
import {PrimaryGeneratedColumn} from "../../../src";
4+
import { TableInheritance } from "../../../src";
5+
import {ChildEntity} from "../../../src";
6+
import {JoinColumn} from "../../../src";
7+
8+
@Entity()
9+
export class Comment {
10+
@PrimaryGeneratedColumn()
11+
id: number;
12+
13+
@Column()
14+
text: string;
15+
16+
@Column()
17+
postId: number;
18+
19+
@ManyToOne(
20+
() => Post,
21+
(entity) => entity.comments,
22+
)
23+
@JoinColumn({
24+
name: "postId",
25+
})
26+
post?: Post;
27+
28+
}
29+
30+
@Entity()
31+
@TableInheritance({column: {type: "string", name: "postType"}})
32+
export class Post {
33+
@PrimaryGeneratedColumn()
34+
id: number;
35+
36+
@Column()
37+
title: string;
38+
39+
@Column()
40+
postType: string = "BasePost";
41+
42+
@OneToMany(() => Comment, (entity) => entity.post)
43+
comments?: Comment[];
44+
}
45+
46+
@ChildEntity("TargetPost")
47+
export class TargetPost extends Post {
48+
@Column()
49+
postType: string = "TargetPost";
50+
}
51+
52+

test/github-issues/6399/issue-6399.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {Connection} from "../../../src";
2+
import {expect} from "chai";
3+
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
4+
import {Post, TargetPost, Comment} from "./entities";
5+
6+
describe("github issues > #6399 Process extraAppendedAndWhereCondition for inherited entity", () => {
7+
let connections: Connection[];
8+
9+
before(async () => {
10+
return connections = await createTestingConnections({
11+
entities: [Post, TargetPost, Comment],
12+
schemaCreate: true,
13+
dropSchema: true,
14+
enabledDrivers: ["mysql"]
15+
});
16+
});
17+
beforeEach(() => reloadTestingDatabases(connections));
18+
after(() => closeTestingConnections(connections));
19+
20+
it("Query with join and limit for inhered entity", () => Promise.all(connections.map(async (connection) => {
21+
const targetPostRepo = connection.getRepository(TargetPost);
22+
23+
const posts: TargetPost[] = [
24+
{
25+
id: 1,
26+
title: "Post 1",
27+
postType: "TargetPost",
28+
},
29+
{ id: 2,
30+
title: "Post 2",
31+
postType: "TargetPost",
32+
},
33+
{
34+
id: 3,
35+
title: "Post 3",
36+
postType: "TargetPost",
37+
},
38+
];
39+
40+
await targetPostRepo.save(posts);
41+
42+
const result = await targetPostRepo.createQueryBuilder("targetPosts")
43+
.leftJoinAndSelect("targetPosts.comments", "comments")
44+
.take(2)
45+
.getMany();
46+
47+
expect(result.length).eq(2);
48+
})));
49+
});

0 commit comments

Comments
 (0)