【问题标题】:NestJS with CRUD decorator column does not exist带有 CRUD 装饰器列的 NestJS 不存在
【发布时间】:2020-07-24 08:50:12
【问题描述】:

我有一个使用 NestJS 的 Rest API 服务器和控制器上带有 CRUD 装饰器的 typeorm。 我有一个用户实体

userEntity {
@PrimaryGeneratedColumn()
  id: number

  @PrimaryColumn({ unique: true })
  username: string


  @Column({ nullable: true })
  email: string

  @Column({ nullable: true })
  phone: string

  @PrimaryColumn()
  role: string
}
  • 我知道用户名也是主列(我有更多列),但我需要通过用户名和角色(或 ID)来识别用户

我有另一个实体(家庭信息)

FamilyInfoEntity {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ nullable: true })
  familyName: string

  @Column({ nullable: true })
  parentId?: number

  @OneToOne(() => UserEntity, parent => parent.id, { nullable: true })
  @JoinColumn()
  parent?: UserEntity
} 

但我收到错误“列 FamilyInfoEntity.parent_username 不存在” 当我试图获取所有家庭信息时(localhost:3000/api/family-info)

我的数据库是空的,用户表上没有行,家庭信息表上也没有行... bot 我不认为这是问题

谢谢。

【问题讨论】:

    标签: node.js nestjs typeorm


    【解决方案1】:

    你需要用@Entity()注解标记每个类

    @Entity()
    class FamilyInfoEntity {
      @PrimaryGeneratedColumn()
      id: number
    
      @Column({ nullable: true })
      familyName: string
    
      @Column({ nullable: true })
      parentId?: number
    
      @OneToOne(() => UserEntity, parent => parent.id, { nullable: true })
      @JoinColumn()
      parent?: UserEntity
    } ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-25
      • 2021-01-20
      • 1970-01-01
      • 2020-06-26
      • 2021-10-25
      • 2021-12-30
      • 2018-07-27
      • 2021-04-11
      相关资源
      最近更新 更多