Skip to content

Commit

Permalink
fix: correctly get referenceColumn value in getEntityValueMap (#7005)
Browse files Browse the repository at this point in the history
* test: add test case (#7002)

* fix: correctly get referenceColumn value in `getEntityValueMap`
  • Loading branch information
dolsup committed Mar 8, 2021
1 parent 8b72d79 commit 7fe723b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/metadata/ColumnMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}, {});
Expand Down
19 changes: 19 additions & 0 deletions test/github-issues/7002/entity/Bar.ts
Original file line number Diff line number Diff line change
@@ -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;
}
25 changes: 25 additions & 0 deletions test/github-issues/7002/entity/Foo.ts
Original file line number Diff line number Diff line change
@@ -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;
}
40 changes: 40 additions & 0 deletions test/github-issues/7002/issue-7002.ts
Original file line number Diff line number Diff line change
@@ -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,
})
);
})
));
});

0 comments on commit 7fe723b

Please sign in to comment.