Skip to content

Commit

Permalink
fix: include asExpression columns in returning clause (#10632)
Browse files Browse the repository at this point in the history
* fix: include asExpression columns in returning clause

Closes: #8450

* test: add test for issue #8450
  • Loading branch information
maartensijmkens authored Jan 26, 2024
1 parent 8aa8690 commit f232ba7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/metadata/EntityMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ export class EntityMetadata {
return this.columns.filter((column) => {
return (
column.default !== undefined ||
column.asExpression !== undefined ||
column.isGenerated ||
column.isCreateDate ||
column.isUpdateDate ||
Expand Down
7 changes: 6 additions & 1 deletion src/query-builder/ReturningResultsEntityUpdator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,11 @@ export class ReturningResultsEntityUpdator {
getUpdationReturningColumns(): ColumnMetadata[] {
return this.expressionMap.mainAlias!.metadata.columns.filter(
(column) => {
return column.isUpdateDate || column.isVersion
return (
column.asExpression !== undefined ||
column.isUpdateDate ||
column.isVersion
)
},
)
}
Expand All @@ -286,6 +290,7 @@ export class ReturningResultsEntityUpdator {
return this.expressionMap.mainAlias!.metadata.columns.filter(
(column) => {
return (
column.asExpression !== undefined ||
column.isUpdateDate ||
column.isVersion ||
column.isDeleteDate
Expand Down
16 changes: 16 additions & 0 deletions test/github-issues/8450/entity/UserEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Entity } from "../../../../src/decorator/entity/Entity"
import { PrimaryColumn } from "../../../../src/decorator/columns/PrimaryColumn"
import { Column } from "../../../../src/decorator/columns/Column"

@Entity("user")
export class UserEntity {
@PrimaryColumn("int")
id: number

@Column({
type: "int",
generatedType: "STORED",
asExpression: "id * 2",
})
generated: number
}
38 changes: 38 additions & 0 deletions test/github-issues/8450/issue-8450.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import "reflect-metadata"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { UserEntity } from "./entity/UserEntity"
import { expect } from "chai"
import { DataSource } from "../../../src"

describe("github issues > #8450 Generated column not in RETURNING clause on save", () => {
let connections: DataSource[]

before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["postgres", "mysql"],
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))

it("should populate an object with generated column values after saving", () =>
Promise.all(
connections.map(async (connection) => {
const user = new UserEntity()
user.id = 100

expect(user.generated).to.be.undefined

await connection.manager.save(user)

expect(user.generated).to.be.a("number")
expect(user.generated).to.be.equal(user.id * 2)
}),
))
})

0 comments on commit f232ba7

Please sign in to comment.