Skip to content

Commit

Permalink
fix: single table inheritance returns the same discriminator value er…
Browse files Browse the repository at this point in the history
…ror for unrelated tables where their parents extend from the same entity (#8525)

* fix: Single table inheritance returns the same discriminator value error for unrelated tables where their parents extend from the
 same entity

Add a new condition in entity metada validator to be sure that we are testing the same table

Solve: #8522

* fix: Add internal role entity

Add missing entity to the last commit

Solve: #8522

* fix: Related tables still failed when have the same discriminator

Add a new test to prove that related tables failed when they have the same discriminator

Solve: #8522
  • Loading branch information
marypaz-sama committed Jan 15, 2022
1 parent cefddd9 commit 6523fcc
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/metadata-builder/EntityMetadataValidator.ts
Expand Up @@ -69,6 +69,7 @@ export class EntityMetadataValidator {
const sameDiscriminatorValueEntityMetadata = allEntityMetadatas.find(metadata => {
return metadata !== entityMetadata
&& (metadata.inheritancePattern === "STI" || metadata.tableType === "entity-child")
&& metadata.tableName === entityMetadata.tableName
&& metadata.discriminatorValue === entityMetadata.discriminatorValue
&& metadata.inheritanceTree.some(parent => entityMetadata.inheritanceTree.indexOf(parent) !== -1);
});
Expand Down
13 changes: 13 additions & 0 deletions test/github-issues/8522/entity/BaseEntity.ts
@@ -0,0 +1,13 @@
import { PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from "../../../../src";

export class BaseEntity {
@PrimaryGeneratedColumn()
id: number;

@CreateDateColumn()
createdAt: Date;

@UpdateDateColumn()
updatedAt: Date;

}
6 changes: 6 additions & 0 deletions test/github-issues/8522/entity/ClientRole.ts
@@ -0,0 +1,6 @@
import { ChildEntity } from "../../../../src";
import { Role } from "./Role";

@ChildEntity('internal')
export class ClientRole extends Role {
}
6 changes: 6 additions & 0 deletions test/github-issues/8522/entity/InternalRole.ts
@@ -0,0 +1,6 @@
import { ChildEntity } from "../../../../src";
import { Role } from "./Role";

@ChildEntity('internal')
export class InternalRole extends Role {
}
6 changes: 6 additions & 0 deletions test/github-issues/8522/entity/InternalUser.ts
@@ -0,0 +1,6 @@
import { ChildEntity } from "../../../../src";
import { User } from "./User";

@ChildEntity('internal')
export class InternalUser extends User {
}
14 changes: 14 additions & 0 deletions test/github-issues/8522/entity/Role.ts
@@ -0,0 +1,14 @@
import { TableInheritance, Column, Entity } from "../../../../src";

import { BaseEntity } from "./BaseEntity";

@Entity()
@TableInheritance({ column: { type: 'varchar', name: 'type' } })
export class Role extends BaseEntity {
@Column()
name: string;

@Column()
description: string;

}
13 changes: 13 additions & 0 deletions test/github-issues/8522/entity/User.ts
@@ -0,0 +1,13 @@
import { TableInheritance, Column, Entity } from "../../../../src";
import { BaseEntity } from "./BaseEntity";

@Entity()
@TableInheritance({ column: { type: 'varchar', name: 'type' } })
export abstract class User extends BaseEntity {
@Column()
firstName: string;

@Column()
lastName: string;

}
90 changes: 90 additions & 0 deletions test/github-issues/8522/issue-8522.ts
@@ -0,0 +1,90 @@
import "reflect-metadata";
import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { Connection } from "../../../src/connection/Connection";
import { expect } from "chai";
import { InternalUser } from "./entity/InternalUser";
import { InternalRole } from "./entity/InternalRole";
import { User } from "./entity/User";
import { Role } from "./entity/Role";
import { BaseEntity, TypeORMError } from "../../../src";
import { ClientRole } from "./entity/ClientRole";
import { afterEach } from "mocha";

describe("github issues > #8522 Single table inheritance returns the same discriminator value error for unrelated tables where their parents extend from the same entity", () => {
let connections: Connection[];

after(() => closeTestingConnections(connections));
afterEach(() => closeTestingConnections(connections));

describe("Unrelated tables",()=>{
before(
async () =>
(connections = await createTestingConnections({
entities: [BaseEntity, InternalUser, InternalRole, Role, User],
schemaCreate: true,
dropSchema: true,
}))
);
beforeEach(() => reloadTestingDatabases(connections));

it("should loads internal user and internal role", () => Promise.all(connections.map(async connection => {
const id = 1;
const date = new Date();

const firstName = "Jane";
const lastName = "Walker";

const name = "admin";
const description = "All permissions";

const internalUser = new InternalUser();
internalUser.id = id;
internalUser.firstName = firstName;
internalUser.lastName = lastName;
internalUser.createdAt = date;
internalUser.updatedAt = date;

await connection.manager.save(internalUser);

const internalRole = new InternalRole();
internalRole.id = id;
internalRole.name = name;
internalRole.description = description;
internalRole.createdAt = date;
internalRole.updatedAt = date;

await connection.manager.save(internalRole);

let users = await connection.manager
.createQueryBuilder(User, "user")
.getMany();

expect(users[0].id).to.be.equal(id);
expect(users[0].firstName).to.be.equal(firstName);
expect(users[0].lastName).to.be.equal(lastName);
expect(users[0].createdAt.should.be.instanceOf(Date));
expect(users[0].updatedAt.should.be.instanceOf(Date));

let roles = await connection.manager
.createQueryBuilder(Role, "role")
.getMany();

expect(roles[0].id).to.be.equal(id);
expect(roles[0].name).to.be.equal(name);
expect(roles[0].description).to.be.equal(description);
expect(roles[0].createdAt.should.be.instanceOf(Date));
expect(roles[0].updatedAt.should.be.instanceOf(Date));
})));
});

describe("Related tables", () => {

it("Should throw error when related tables have the same discriminator", async () =>{
await createTestingConnections({
entities: [BaseEntity, ClientRole, InternalRole, Role, User],
schemaCreate: true,
dropSchema: true,
}).should.be.rejectedWith(TypeORMError,`Entities ClientRole and InternalRole have the same discriminator values. Make sure they are different while using the @ChildEntity decorator.`);
});
});
});

0 comments on commit 6523fcc

Please sign in to comment.