Skip to content

Commit

Permalink
fix: save does not return id, save does not return generated (#7336)
Browse files Browse the repository at this point in the history
* fix save does not return id

* allow subject.generatedMap to be null/undefined

* stop using coalescing operator

* actually allow generated map to bu undefined

* add test

* use doublequotes

* remove singlequotes

* fix formatting
  • Loading branch information
TheBlue-1 committed Feb 6, 2021
1 parent e06a442 commit 01a6aee
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/persistence/SubjectExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,19 @@ export class SubjectExecutor {
}

const updateResult = await updateQueryBuilder.execute();
subject.generatedMap = updateResult.generatedMaps[0];
if (subject.generatedMap) {
let updateGeneratedMap = updateResult.generatedMaps[0];
if (updateGeneratedMap) {
subject.metadata.columns.forEach(column => {
const value = column.getEntityValue(subject.generatedMap!);
const value = column.getEntityValue(updateGeneratedMap!);
if (value !== undefined && value !== null) {
const preparedValue = this.queryRunner.connection.driver.prepareHydratedValue(value, column);
column.setEntityValue(subject.generatedMap!, preparedValue);
column.setEntityValue(updateGeneratedMap!, preparedValue);
}
});
if (!subject.generatedMap) {
subject.generatedMap = {};
}
Object.assign(subject.generatedMap, updateGeneratedMap);
}

// experiments, remove probably, need to implement tree tables children removal
Expand Down
9 changes: 9 additions & 0 deletions test/github-issues/5520/entity/TestChild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src";

@Entity()
export class TestChild {
@Column()
public value: string;
@PrimaryGeneratedColumn("uuid")
public uuid: string;
}
15 changes: 15 additions & 0 deletions test/github-issues/5520/entity/TestParent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn } from "../../../../src";
import { TestChild } from "./TestChild";

@Entity()
export class TestParent {
@OneToOne("TestChild", {
nullable: true,
eager: true,
cascade: true,
})
@JoinColumn()
public child: TestChild;
@PrimaryGeneratedColumn("uuid")
public uuid: string;
}
45 changes: 45 additions & 0 deletions test/github-issues/5520/issue-5520.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import "reflect-metadata";

import { assert } from "chai";

import { Connection } from "../../../src";
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { TestChild } from "./entity/TestChild";
import { TestParent } from "./entity/TestParent";

describe("github issues > #5520 save does not return generated id if object to save contains a many to one relationship with an undefined id", () => {
let connections: Connection[];
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
}))
);
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should generate parents and childs uuid and return them", () =>
Promise.all(
connections.map(async (connection) => {
let entity = new TestParent();
let entityChild = new TestChild();
entityChild.value = "test";
entity.child = entityChild;

let response = await connection
.getRepository(TestParent)
.save(entity);

assert(
response.uuid,
"parent uuid should be generated and set"
);
assert(
response.child.uuid,
"child uuid should be generated and set"
);
})
));
});

0 comments on commit 01a6aee

Please sign in to comment.