Skip to content

Commit af77a5d

Browse files
authored
fix: default value in child table/entity column decorator for multiple table inheritance is ignored for inherited columns (#10563) (#10564)
1 parent aa8d24c commit af77a5d

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

src/metadata-builder/EntityMetadataBuilder.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,21 @@ export class EntityMetadataBuilder {
542542
(column) => column.propertyName === args.propertyName,
543543
)!
544544

545+
// for multiple table inheritance we can override default column values
546+
if (
547+
entityMetadata.tableType === "regular" &&
548+
args.target !== entityMetadata.target
549+
) {
550+
const childArgs = this.metadataArgsStorage.columns.find(
551+
(c) =>
552+
c.propertyName === args.propertyName &&
553+
c.target === entityMetadata.target,
554+
)
555+
if (childArgs && childArgs.options.default) {
556+
args.options.default = childArgs.options.default
557+
}
558+
}
559+
545560
const column = new ColumnMetadata({
546561
connection: this.connection,
547562
entityMetadata,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Column, Entity, PrimaryColumn } from "../../../../src"
2+
3+
@Entity()
4+
export class FamilyMember {
5+
@PrimaryColumn()
6+
name: string
7+
8+
@Column({
9+
default: "PERSON",
10+
})
11+
type: string
12+
}
13+
14+
@Entity()
15+
export class Dog extends FamilyMember {
16+
@PrimaryColumn()
17+
name: string
18+
19+
@Column({
20+
default: "PET",
21+
})
22+
type: string
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import "reflect-metadata"
2+
import {
3+
createTestingConnections,
4+
closeTestingConnections,
5+
reloadTestingDatabases,
6+
} from "../../utils/test-utils"
7+
import { DataSource } from "../../../src/data-source/DataSource"
8+
import { assert } from "chai"
9+
import { Dog } from "./entity/family"
10+
11+
describe("github issues > #10653 Default value in child table/entity column decorator for multiple table inheritance is ignored for inherited columns", () => {
12+
let dataSources: DataSource[]
13+
before(
14+
async () =>
15+
(dataSources = await createTestingConnections({
16+
entities: [__dirname + "/entity/*{.js,.ts}"],
17+
schemaCreate: true,
18+
dropSchema: true,
19+
})),
20+
)
21+
beforeEach(() => reloadTestingDatabases(dataSources))
22+
after(() => closeTestingConnections(dataSources))
23+
24+
it("should honor distinct default value configured on inherited column of child entity", () =>
25+
Promise.all(
26+
dataSources.map(async (dataSource) => {
27+
await Promise.all(
28+
dataSources.map(async (dataSource) => {
29+
const manager = dataSource.manager
30+
let dog: Dog = new Dog()
31+
dog.name = "Fifi"
32+
await manager.save(dog)
33+
let fifi = await manager.findOneBy(Dog, {
34+
name: "Fifi",
35+
})
36+
assert(
37+
fifi instanceof Dog && fifi["type"] == "PET",
38+
`Fifi=${JSON.stringify(fifi)}`,
39+
)
40+
}),
41+
)
42+
}),
43+
))
44+
})

0 commit comments

Comments
 (0)