diff --git a/src/metadata/ColumnMetadata.ts b/src/metadata/ColumnMetadata.ts index 42447ad7ec..a2631768e1 100644 --- a/src/metadata/ColumnMetadata.ts +++ b/src/metadata/ColumnMetadata.ts @@ -544,9 +544,9 @@ export class ColumnMetadata { return Object.keys(map).length > 0 ? map : undefined; } else { // no embeds - no problems. Simply return column property name and its value of the entity - if (this.relationMetadata && entity[this.propertyName] && entity[this.propertyName] instanceof Object) { + if (this.relationMetadata && entity[this.relationMetadata.propertyName] && entity[this.relationMetadata.propertyName] instanceof Object) { const map = this.relationMetadata.joinColumns.reduce((map, joinColumn) => { - const value = joinColumn.referencedColumn!.getEntityValueMap(entity[this.propertyName]); + const value = joinColumn.referencedColumn!.getEntityValueMap(entity[this.relationMetadata!.propertyName]); if (value === undefined) return map; return OrmUtils.mergeDeep(map, value); }, {}); diff --git a/test/github-issues/7002/entity/Bar.ts b/test/github-issues/7002/entity/Bar.ts new file mode 100644 index 0000000000..275d05f866 --- /dev/null +++ b/test/github-issues/7002/entity/Bar.ts @@ -0,0 +1,19 @@ +import { + Column, + Entity, + OneToOne, + PrimaryGeneratedColumn, +} from "../../../../src"; +import { Foo } from "./Foo"; + +@Entity() +export class Bar { + @PrimaryGeneratedColumn("increment") + id: number; + + @Column() + title: string; + + @OneToOne(() => Foo, foo => foo.bar, { cascade: true, eager: true }) + foo: Foo; +} diff --git a/test/github-issues/7002/entity/Foo.ts b/test/github-issues/7002/entity/Foo.ts new file mode 100644 index 0000000000..455ea41dcf --- /dev/null +++ b/test/github-issues/7002/entity/Foo.ts @@ -0,0 +1,25 @@ +import { + Column, + CreateDateColumn, + Entity, + JoinColumn, + OneToOne, + PrimaryColumn, +} from "../../../../src"; +import { Bar } from "./Bar"; + +@Entity() +export class Foo { + @PrimaryColumn() + id: number; + + @Column() + text: string; + + @OneToOne(() => Bar, b => b.foo, { primary: true }) + @JoinColumn({ name: "id", referencedColumnName: "id" }) + bar: Bar; + + @CreateDateColumn() + d: Date; +} diff --git a/test/github-issues/7002/issue-7002.ts b/test/github-issues/7002/issue-7002.ts new file mode 100644 index 0000000000..ea040a8e4e --- /dev/null +++ b/test/github-issues/7002/issue-7002.ts @@ -0,0 +1,40 @@ +import "reflect-metadata"; + +import { Connection } from "../../../src/connection/Connection"; +import { + closeTestingConnections, + createTestingConnections, + reloadTestingDatabases, +} from "../../utils/test-utils"; +import { Bar } from "./entity/Bar"; +import { Foo } from "./entity/Foo"; + +describe("github issues > #7002 cascade save fails if the child entity has CreateDateColumn and PK as JoinColumn", () => { + let connections: Connection[]; + before( + async () => + (connections = await createTestingConnections({ + entities: [__dirname + "/entity/*{.js,.ts}"], + schemaCreate: true, + dropSchema: true, + enabledDrivers: ["mysql", "postgres"], + })) + ); + beforeEach(() => reloadTestingDatabases(connections)); + after(() => closeTestingConnections(connections)); + + it("save an entity having a child entity with shared PK and CreatedDateColumn by cascade", () => + Promise.all( + connections.map(async (connection) => { + const foo = new Foo(); + foo.text = "This is a feature post"; + + await connection.manager.save( + connection.getRepository(Bar).create({ + title: "Feature Post", + foo, + }) + ); + }) + )); +});