我在Postgres上使用TypeORM在Batch
和Session
之间建立了一对多的关系。
即,一个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
,这也很糟糕。
如何保存两个相互关联的新实体?