Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Query builder makes query with joins, without limit for inherite…
- Loading branch information
Showing
3 changed files
with
118 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {Entity, OneToMany, ManyToOne} from "../../../src"; | ||
import {Column} from "../../../src"; | ||
import {PrimaryGeneratedColumn} from "../../../src"; | ||
import { TableInheritance } from "../../../src"; | ||
import {ChildEntity} from "../../../src"; | ||
import {JoinColumn} from "../../../src"; | ||
|
||
@Entity() | ||
export class Comment { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
text: string; | ||
|
||
@Column() | ||
postId: number; | ||
|
||
@ManyToOne( | ||
() => Post, | ||
(entity) => entity.comments, | ||
) | ||
@JoinColumn({ | ||
name: "postId", | ||
}) | ||
post?: Post; | ||
|
||
} | ||
|
||
@Entity() | ||
@TableInheritance({column: {type: "string", name: "postType"}}) | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@Column() | ||
postType: string = "BasePost"; | ||
|
||
@OneToMany(() => Comment, (entity) => entity.post) | ||
comments?: Comment[]; | ||
} | ||
|
||
@ChildEntity("TargetPost") | ||
export class TargetPost extends Post { | ||
@Column() | ||
postType: string = "TargetPost"; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {Connection} from "../../../src"; | ||
import {expect} from "chai"; | ||
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils"; | ||
import {Post, TargetPost, Comment} from "./entities"; | ||
|
||
describe("github issues > #6399 Process extraAppendedAndWhereCondition for inherited entity", () => { | ||
let connections: Connection[]; | ||
|
||
before(async () => { | ||
return connections = await createTestingConnections({ | ||
entities: [Post, TargetPost, Comment], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
enabledDrivers: ["mysql"] | ||
}); | ||
}); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("Query with join and limit for inhered entity", () => Promise.all(connections.map(async (connection) => { | ||
const targetPostRepo = connection.getRepository(TargetPost); | ||
|
||
const posts: TargetPost[] = [ | ||
{ | ||
id: 1, | ||
title: "Post 1", | ||
postType: "TargetPost", | ||
}, | ||
{ id: 2, | ||
title: "Post 2", | ||
postType: "TargetPost", | ||
}, | ||
{ | ||
id: 3, | ||
title: "Post 3", | ||
postType: "TargetPost", | ||
}, | ||
]; | ||
|
||
await targetPostRepo.save(posts); | ||
|
||
const result = await targetPostRepo.createQueryBuilder("targetPosts") | ||
.leftJoinAndSelect("targetPosts.comments", "comments") | ||
.take(2) | ||
.getMany(); | ||
|
||
expect(result.length).eq(2); | ||
}))); | ||
}); |