nestjs - TypeORM - 使用枚举鉴别器的表继承
问题描述
尝试使用枚举来区分子实体:
export enum ActionCategory {
USER = 'user',
POSITION = 'position',
NOTE = 'note',
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.USER
})
readonly category: ActionCategory
}
但我遇到了一个错误:
[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
如果我使用默认值,那么我会收到错误
QueryFailedError: column "action_category" of relation "action" already exists
解决方案
TableInheritance
所以我可以通过在装饰中指定列名来规避这个问题:
@TableInheritance({
column: 'category',
})
离开@Column
实体时,这工作正常:
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
}
推荐阅读
- ios - 如何用 6 个三角形 SCNNode 制作六边形?
- python - 在python中围绕数据绘制一个正方形
- groovy - 调用时导入的类在 Groovy Closure 中如何工作?
- python - Python - 从 csv 中选择不同的行值并将它们组合到新的 csv 中
- java - 在活动的 JRE 中找不到“tools.jar”
- python - 如何解决和问题训练 GMM -HMM 进行语音识别?
- django - Django 通过外键搜索子模型
- office-js - 在功能区的同一组标签下显示多个 Outlook 插件
- .htaccess - 在 .hcaccess 文件中重定向时,参数被传输到新的 url
- flutter - InWell 在多个小部件上的实现