Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: single table inheritance returns the same discriminator value er…
…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
1 parent
cefddd9
commit 6523fcc
Showing
8 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from "../../../../src"; | ||
|
||
export class BaseEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; | ||
|
||
@UpdateDateColumn() | ||
updatedAt: Date; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ChildEntity } from "../../../../src"; | ||
import { Role } from "./Role"; | ||
|
||
@ChildEntity('internal') | ||
export class ClientRole extends Role { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ChildEntity } from "../../../../src"; | ||
import { Role } from "./Role"; | ||
|
||
@ChildEntity('internal') | ||
export class InternalRole extends Role { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ChildEntity } from "../../../../src"; | ||
import { User } from "./User"; | ||
|
||
@ChildEntity('internal') | ||
export class InternalUser extends User { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.`); | ||
}); | ||
}); | ||
}); |