question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Table Inheritance using Enum Discriminator: column of relation already exists

See original GitHub issue

Expected Behavior

Table Inheritance can be used with an enum as discriminator.

Actual Behavior

When trying to use an enum to discriminate the child entities (see code sample in steps to reproduce) I ran into the error:

[Nest] 52222   - 27/01/2021, 10:30:25   [TypeOrmModule] Unable to connect to the database. Retrying (2)... +1ms
QueryFailedError: column "action_category" contains null values
    at new QueryFailedError (/Users/rai/dev/lexstep/lexstep-nest/node_modules/.pnpm/typeorm@0.2.29/node_modules/typeorm/error/QueryFailedError.js:11:28)
I can avoid this issue by changing the name of one of the columns (either the @TableInheritance 'action_category' or the @Column 'action_category' but this results in an extra column being created

So I changed the @Column definition to use a default value, which silenced this error, but then produced:

QueryFailedError: column "action_category" of relation "action" already exists

Steps to Reproduce

Using the following entity definition:

export enum ActionCategory {
  SYSTEM = 'system',
  EMAIL = 'email',
}

@Index('action_pkey', ['id'], { unique: true })
@Entity()
@TableInheritance({
  column: { type: 'enum', enum: ActionCategory, name: 'category' },
})
export class Action {
  @PrimaryGeneratedColumn({
    name: 'action_id',
    type: 'bigint',
  })
  readonly id: number

  @Column({
    name: 'category',
    type: 'enum',
    enum: ActionCategory,
    default: ActionCategory.SYSTEM
  })
  readonly category: ActionCategory
}

@ChildEntity(ActionCategory.SYSTEM)
export class SystemAction extends Action {
  @Column({ name: 'message', type: 'varchar', nullable: true })
  readonly message?: string
}

@ChildEntity(ActionCategory.EMAIL)
export class EmailAction extends Action {
  @Column({ name: 'email', type: 'varchar', nullable: true })
  readonly email?: string
}

…in any TypeORM project you can see the errors for yourself.

My Environment

Dependency Version
Operating System TempleOS
Node.js version v14.12.0
Typescript version v4.1.3
TypeORM version v0.2.29

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
rbuteracommented, Jan 27, 2021

So I was able to circumvent this issue by specifying the column name in the TableInheritance decoration:

@TableInheritance({
  column: 'category',
})

this worked fine when leaving the @Column in the entity:

export class Action extends Timestamped {
  @PrimaryGeneratedColumn('uuid', {
    name: 'action_id',
  })
  readonly id: string

  @Column({
    name: 'action_category',
    type: 'enum',
    default: ActionCategory.SYSTEM,
    enum: ActionCategory,
  })
  readonly category: ActionCategory
}

I can close this issue if appropriate, but I think typeORM should have some sort of warning/error for the case where the TableInheritance column name matches the column name of an entity’s explicitly defined columns.

Any pointers for implementing such a warning/error? I’d like to contribute to the project.

0reactions
CarlosFandangocommented, Aug 15, 2021

So I was able to circumvent this issue by specifying the column name in the TableInheritance decoration:

@TableInheritance({
  column: 'category',
})

this worked fine when leaving the @Column in the entity:

export class Action extends Timestamped {
  @PrimaryGeneratedColumn('uuid', {
    name: 'action_id',
  })
  readonly id: string

  @Column({
    name: 'action_category',
    type: 'enum',
    default: ActionCategory.SYSTEM,
    enum: ActionCategory,
  })
  readonly category: ActionCategory
}

I can close this issue if appropriate, but I think typeORM should have some sort of warning/error for the case where the TableInheritance column name matches the column name of an entity’s explicitly defined columns.

Any pointers for implementing such a warning/error? I’d like to contribute to the project.

I had this same problem and removing the typing from the @TableInheritance decorator worked a charm! Thanks!

I declared my enum like this:

@Column({
    type: 'enum',
    enum: CourseTypeEnum,
    default: CourseTypeEnum.STUDIO,
  })
  @Field({ nullable: true })
  type!: CourseTypeEnum;
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeORM - table inheritance using enum discriminator
So I was able to circumvent this issue by specifying the column name in the TableInheritance decoration: @TableInheritance({ column: ...
Read more >
Mapping Class Inheritance Hierarchies
In joined table inheritance, each class along a hierarchy of classes is represented by a distinct table. Querying for a particular subclass in ......
Read more >
Inheritance Mapping - Doctrine Object Relational Mapper (ORM)
Single Table Inheritance is an inheritance mapping strategy where all classes of a hierarchy are mapped to a single database table. In order...
Read more >
The best way to map the @DiscriminatorColumn with JPA and ...
As previously explained, the SINGLE_TABLE inheritance is the most efficient entity inheritance strategy. However, for JPQL query such as this ...
Read more >
12.8. Field Mapping
@Entity @Table(schema="CNTRCT") @Inheritance(strategy=InheritanceType.JOINED) @DiscriminatorColumn(name="CTYPE") @AttributeOverride(name="version", column=@ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found