You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeORM使用@TableInheritance时如何自定义自动生成的列索引名称

解决方案

你可以通过以下两种方式自定义该自动生成的索引名称:

方式1:在已有的type列定义中显式指定索引名(推荐)

你已经单独声明了type列,直接在@Column装饰器的配置项中添加index属性指定名称即可,TypeORM会优先使用你自定义的索引配置,不会再生成默认哈希名的索引:

@TableInheritance({ column: 'type' })
export abstract class Node extends Base {
  @PrimaryGeneratedColumn({ name: 'id' })
  id!: number

  @Column({
    length: '64',
    // 新增这部分配置自定义索引名
    index: {
      name: 'idx_node_type'
    }
  })
  type!: string
}

方式2:直接在@TableInheritance中配置完整的鉴别列参数

你也可以把type列的所有配置都放到@TableInheritance的column属性中,不需要再单独声明type列:

@TableInheritance({ 
  column: {
    name: 'type',
    type: 'varchar',
    length: 64,
    index: {
      name: 'idx_node_type'
    }
  }
})
export abstract class Node extends Base {
  @PrimaryGeneratedColumn({ name: 'id' })
  id!: number
}

注意:如果同时使用两种配置方式,请保证两边的列属性完全一致,避免出现配置冲突。

内容的提问来源于stack exchange,提问作者Val.istar.Guo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 03:54:06