【问题标题】:Issue of Primary key in TypeORMTypeORM 中的主键问题
【发布时间】:2022-01-07 19:45:08
【问题描述】:

我有一个BaseEntity,它对多个模块很常见。所以我在这个实体类中创建了一些通用列。所有的类都会扩展这个类。

export abstract class BaseEntity {
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @CreateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
    createdAt: Date;

    @UpdateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
    modifiedAt: Date;

    @DeleteDateColumn({ type: 'timestamp' })
    deletedAt: Date;
}

我的Certificate 类正在扩展BaseEntity。现在我想自动生成certificateNo 的值。

Entity({ name: 'certificate' })
export class Certificate extends BaseEntity {


    @ApiProperty()
    @Generated('increment')
    certificateNo: string;

    @ApiProperty()
    @Column({ type: 'varchar', length: 100 })
    requestStatus: RequestStatus;

    @ApiProperty()
    @Column({ type: 'varchar', length: 100 })
    sponser: string;
}

在我将 @Column() 装饰器放入 certificateNo 列时,它会给出错误。否则此列不会在数据库中创建。 DB 是 postgres。

    @ApiProperty()
    @Column()   //if I write @Column() error comes. If I dont write ,column not created in DB
    @Generated('increment')
    certificateNo: string;

错误是:-

[Nest] 24080   - 12/01/2021, 12:59:35 PM   [ExceptionHandler] syntax error at or near "NOT" +4ms
QueryFailedError: syntax error at or near "NOT"

【问题讨论】:

    标签: node.js typescript nestjs typeorm typeorm-datamapper


    【解决方案1】:

    您应该为“certificateNo”属性指定正确的列类型。见下文:

    @Entity({ name: "certificate" })
    export class Certificate extends BaseEntity {
    
      @Column({type: "integer"})
      @Generated("increment")
      certificateNo: number;
    
      @Column({ type: "varchar", length: 100 })
      requestStatus: string;
    
      @Column({ type: "varchar", length: 100 })
      sponser: string;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-21
      • 2020-12-11
      • 2013-11-22
      • 2021-04-26
      • 1970-01-01
      • 2012-06-05
      • 2011-06-30
      • 1970-01-01
      相关资源
      最近更新 更多