TypeORM:如何保存两个相互关联的新实体?

时间:2020-06-11 17:17:57

标签: postgresql typeorm

我在Postgres上使用TypeORM在BatchSession之间建立了一对多的关系。

即,一个Batch可能有许多Sessions


@Entity()
export class WorkshopBatch extends BaseEntity {
  ...

  @OneToMany((type) => WorkshopSession, (session) => session.uuid)
  // A Series take place over many days and contains multiple sessions
  sessions: WorkshopSession[];

  ...
}

每个会话都属于一个批次:

@entity
export class WorkshopSession extends BaseEntity {

  ...
  @Index()
  @ManyToOne((type) => WorkshopBatch, (batch) => batch.sessions)
  @JoinColumn()
  @IsNotEmpty()
  // The batch this individual workshop belongs to
  batch: WorkshopBatch;

  ...

当我尝试为新的WorkshopSessions创建Batch时,由于WorkshopSession字段为空,因此无法保存batch

- property batch has failed the following constraints: isNotEmpty

我了解。但是,如果我首先制作Batch,它将没有任何Sessions,这也很糟糕。

如何保存两个相互关联的新实体?

0 个答案:

没有答案