From 8e0d8179ee1c9a7cbd1d6e04f1e8282c75815e19 Mon Sep 17 00:00:00 2001 From: Bradley Odell Date: Fri, 15 May 2020 02:53:22 -0700 Subject: [PATCH] fix: use an empty string enum as the type of a primary key column (#6063) Closes: #3874 --- .../RawSqlResultsToEntityTransformer.ts | 1 - test/github-issues/3874/entity/Settings.ts | 16 ++++++++++ test/github-issues/3874/issue-3874.ts | 30 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/github-issues/3874/entity/Settings.ts create mode 100644 test/github-issues/3874/issue-3874.ts diff --git a/src/query-builder/transformer/RawSqlResultsToEntityTransformer.ts b/src/query-builder/transformer/RawSqlResultsToEntityTransformer.ts index c1d0d670c0..c10e46facd 100644 --- a/src/query-builder/transformer/RawSqlResultsToEntityTransformer.ts +++ b/src/query-builder/transformer/RawSqlResultsToEntityTransformer.ts @@ -72,7 +72,6 @@ export class RawSqlResultsToEntityTransformer { return keyValue; }).join("_"); // todo: check partial - if (!id) return; const items = map.get(id); if (!items) { diff --git a/test/github-issues/3874/entity/Settings.ts b/test/github-issues/3874/entity/Settings.ts new file mode 100644 index 0000000000..2d3cdc0866 --- /dev/null +++ b/test/github-issues/3874/entity/Settings.ts @@ -0,0 +1,16 @@ +import {Entity, Column, PrimaryColumn} from "../../../../src"; + +enum Singleton { + EMPTY = "" +} + +@Entity() +export class Settings { + + @PrimaryColumn() + readonly singleton: Singleton = Singleton.EMPTY; + + @Column() + value!: string; + +} diff --git a/test/github-issues/3874/issue-3874.ts b/test/github-issues/3874/issue-3874.ts new file mode 100644 index 0000000000..4aa095de4f --- /dev/null +++ b/test/github-issues/3874/issue-3874.ts @@ -0,0 +1,30 @@ +import "reflect-metadata"; +import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils"; +import {Connection} from "../../../src/connection/Connection"; +import {Settings} from "./entity/Settings"; +import {expect} from "chai"; + +describe("github issues > #3874 Using an (empty string) enum as the type of a primary key column", () => { + + let connections: Connection[]; + before(async () => connections = await createTestingConnections({ + entities: [Settings], + enabledDrivers: ["mysql", "mariadb"], + schemaCreate: true, + dropSchema: true, + })); + beforeEach(() => reloadTestingDatabases(connections)); + after(() => closeTestingConnections(connections)); + + it("should reload entity", () => Promise.all(connections.map(async connection => { + // Create initial settings row + const newSettings = new Settings(); + newSettings.value = "string"; + await connection.manager.save(newSettings); + // Attempt to read settings back + const foundSettings = await connection.manager.findOne(Settings); + expect(foundSettings).to.be.an.instanceOf(Settings); + expect(foundSettings != null ? foundSettings.value : null).to.equal("string"); + }))); + +});