Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single table inheritance returns the same discriminator value error for unrelated tables where their parents extend from the same entity #8522

Closed
2 of 21 tasks
marypaz-sama opened this issue Jan 11, 2022 · 0 comments · Fixed by #8525

Comments

@marypaz-sama
Copy link
Contributor

Issue Description

There are two unrelated child entities following the single table inheritance pattern with same discriminator value, if both entities inherit from a class that just adds some common columns it, it throws an error.

For example:


             BaseEntity
           /            \
        Role           User
         |               |
     InternalRole    InternalUser

Both InternalRole and InternalRole entities have the same discriminator value internal.
BaseEntity just defines some common columns.

Expected Behavior

There should be no error, as the entities InternalUser and InternalRole are not related because BaseEntity is just a generic entity to create createdAt, updatedAt and primary columns for all tables. And TableInheritance decorator is added in User and Role entities not BaseEntity

Actual Behavior

It throws an error.

Error: Entities InternalUser and InternalRole have the same discriminator values. Make sure they are different while using the @ChildEntity decorator.

Steps to Reproduce

Minimal code reproduction:

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

  @CreateDateColumn()
  createdAt: Date;

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

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

  @Column()
  description: string;
}
@ChildEntity('internal')
export class InternalRole extends Role {}

My Environment

Dependency Version
Operating System macOs
Node.js version v16.13.1
Typescript version v4.1.3
TypeORM version v0.2.41
NestJS Version v8.1.8

Additional Context

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
marypaz-sama pushed a commit to marypaz-sama/typeorm that referenced this issue Jan 12, 2022
…ror 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: typeorm#8522
@marypaz-sama marypaz-sama changed the title Single table inheritance returns the same discriminator value error for unrelated tables where their parents extend from the same table Single table inheritance returns the same discriminator value error for unrelated tables where their parents extend from the same entity Jan 12, 2022
marypaz-sama pushed a commit to marypaz-sama/typeorm that referenced this issue Jan 12, 2022
Add missing entity to the last commit

Solve: typeorm#8522
marypaz-sama pushed a commit to marypaz-sama/typeorm that referenced this issue Jan 12, 2022
Add a new test to prove that related tables failed when they have the same discriminator

Solve: typeorm#8522
pleerock pushed a commit that referenced this issue Jan 15, 2022
…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
TheNoim added a commit to TheNoim/typeorm that referenced this issue Jan 30, 2022
The test for issue typeorm#8522 didn't probably close all of its connections. This could result in test for issue typeorm#863 to fail. With this change the connections should all probably get closed.

Closes typeorm#8567
TheNoim added a commit to TheNoim/typeorm that referenced this issue Jan 30, 2022
The test for issue typeorm#8522 didn't probably close all of its connections. This could result in test for issue typeorm#863 to fail. With this change the connections should all probably get closed.

Closes typeorm#8567
TheNoim added a commit to TheNoim/typeorm that referenced this issue Jan 30, 2022
The test for issue typeorm#8522 didn't probably close all of its connections. This could result in test for issue typeorm#863 to fail. With this change the connections should all probably get closed.

Closes typeorm#8567
TheNoim added a commit to TheNoim/typeorm that referenced this issue Jan 30, 2022
The test for issue typeorm#8522 didn't probably close all of its connections. This could result in test for issue typeorm#863 to fail. With this change the connections should all probably get closed.

Closes typeorm#8567
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant