【问题标题】:typeorm: how to use manytoone with just one column of primary key?typeorm:如何使用只有一列主键的 manytoone?
【发布时间】:2021-05-05 09:58:03
【问题描述】:

考虑以下实体:

@Entity()
export class One {
  @ManyToOne(() => Two, two => two.oneSomeString)
  @PrimaryColumn()
  public readonly someString!: string

  @PrimaryColumn()
  public readonly someNumber!: number
}

@Entity()
export class Two {
  @PrimaryGeneratedColumn()
  id!: number

  @OneToMany(() => One, one => one.someString, { cascade: true })
  @Column()
  public readonly oneSomeString!: string

我希望Two 的每条记录仅通过someString 而不是整个PK 识别One 的多条记录(因此仅通过someString 进行级联删除)。

【问题讨论】:

    标签: postgresql typeorm


    【解决方案1】:

    试试这样的:

    @Entity()
    export class One {
      @PrimaryColumn()
      public readonly someString!: string
    
      @PrimaryColumn()
      public readonly someNumber!: number
    
      @ManyToOne(() => Two, two => two.ones)
      @JoinColumn({ name: 'someString' })
      public readonly two: Two
    }
    
    @Entity()
    export class Two {
      @PrimaryGeneratedColumn()
      id!: number
    
      @Column()
      public readonly oneSomeString!: string
    
      @OneToMany(() => One, one => one.two, { cascade: true })
      public readonly ones: One[]
    

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 2020-10-18
      • 2015-07-16
      • 2020-07-01
      • 1970-01-01
      • 2022-01-07
      • 2012-11-25
      • 2015-03-07
      • 2021-10-21
      相关资源
      最近更新 更多