Skip to content

Commit 022d2b5

Browse files
authored
fix: aggregate function throw error when column alias name is set (#10035)
* fix: aggregate function throw error when column alias name is set Closes: #9927
1 parent c6f608d commit 022d2b5

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

src/entity-manager/EntityManager.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,11 +1054,20 @@ export class EntityManager {
10541054
where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[] = {},
10551055
): Promise<number | null> {
10561056
const metadata = this.connection.getMetadata(entityClass)
1057+
const column = metadata.columns.find(
1058+
(item) => item.propertyPath === columnName,
1059+
)
1060+
if (!column) {
1061+
throw new TypeORMError(
1062+
`Column "${columnName}" was not found in table "${metadata.name}"`,
1063+
)
1064+
}
1065+
10571066
const result = await this.createQueryBuilder(entityClass, metadata.name)
10581067
.setFindOptions({ where })
10591068
.select(
10601069
`${fnName}(${this.connection.driver.escape(
1061-
String(columnName),
1070+
column.databaseName,
10621071
)})`,
10631072
fnName,
10641073
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Entity } from "../../../../src/decorator/entity/Entity"
2+
import { Column } from "../../../../src/decorator/columns/Column"
3+
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn"
4+
5+
@Entity()
6+
export class ExampleEntity {
7+
@PrimaryGeneratedColumn()
8+
id: number
9+
10+
@Column({
11+
type: "int",
12+
name: "serial_no_id",
13+
nullable: false,
14+
default: 0,
15+
})
16+
serialNoId: number
17+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { DataSource } from "../../../src"
2+
import {
3+
closeTestingConnections,
4+
createTestingConnections,
5+
reloadTestingDatabases,
6+
} from "../../utils/test-utils"
7+
import { ExampleEntity } from "./entity/ExampleEntity"
8+
9+
describe("github issues > #9927 aggregate function throw error when column alias name is set", () => {
10+
let dataSources: DataSource[]
11+
12+
before(async () => {
13+
dataSources = await createTestingConnections({
14+
entities: [ExampleEntity],
15+
enabledDrivers: ["mariadb"],
16+
})
17+
})
18+
19+
beforeEach(() => reloadTestingDatabases(dataSources))
20+
after(() => closeTestingConnections(dataSources))
21+
22+
it("should call `maximum` method successfully", async () => {
23+
await Promise.all(
24+
dataSources.map(async (dataSource) => {
25+
await dataSource.manager.maximum(
26+
ExampleEntity,
27+
"serialNoId",
28+
{},
29+
)
30+
}),
31+
)
32+
})
33+
34+
it("should call `minimum` method successfully", async () => {
35+
await Promise.all(
36+
dataSources.map(async (dataSource) => {
37+
await dataSource.manager.minimum(
38+
ExampleEntity,
39+
"serialNoId",
40+
{},
41+
)
42+
}),
43+
)
44+
})
45+
46+
it("should call `sum` method successfully", async () => {
47+
await Promise.all(
48+
dataSources.map(async (dataSource) => {
49+
await dataSource.manager.sum(ExampleEntity, "serialNoId", {})
50+
}),
51+
)
52+
})
53+
54+
it("should call `average` method successfully", async () => {
55+
await Promise.all(
56+
dataSources.map(async (dataSource) => {
57+
await dataSource.manager.average(
58+
ExampleEntity,
59+
"serialNoId",
60+
{},
61+
)
62+
}),
63+
)
64+
})
65+
})

0 commit comments

Comments
 (0)