TypeORM - 一对多:EntityColumnNotFound:未找到实体列“context.physicalPresets”

Amb*_*ier 6 typeorm

更新Project实体时出现以下错误:EntityColumnNotFound: No entity column "context.physicalPresets" was found.

ProjectContextProject如此处所述嵌入https://typeorm.io/#/embedded-entitiesProjectContextPhysical实体有一个 OneToMany 关系。

@Entity()
export class Project extends BaseEntityModel {
  // embedded
  @Column(type => ProjectContext)
  context: ProjectContext;
}
Run Code Online (Sandbox Code Playgroud)
// embedded entity
@Entity()
export class ProjectContext {
  @OneToMany(type => Physical, physical => physical.project, {
    eager: true,
  })
  @JoinColumn()
  physicalPresets: Physical[];
}
Run Code Online (Sandbox Code Playgroud)
@Entity()
export class Physical extends BaseEntityModel {
  @ManyToOne(
    type => Project, project => project.context.physicalPresets,
    {onDelete: 'CASCADE'},
  )
  project: Project;
}
Run Code Online (Sandbox Code Playgroud)

没有更多的成功:

@Entity()
export class Physical extends BaseEntityModel {
 @ManyToOne(
    type => ProjectContext, projectContext => projectContext.physicalPresets,
    {onDelete: 'CASCADE'},
  )
  projectContext: ProjectContext;
}
Run Code Online (Sandbox Code Playgroud)

我明白了Entity metadata for Physical#projectContext was not found. Check if you specified a correct entity object and if it's connected in the connection options

  async update(entity: Project): Promise<UpdateResult> {
    return await this.repository.update(entity.id, entity);
  }
Run Code Online (Sandbox Code Playgroud)

是否可以在嵌入实体中具有 OneToMany 关系,如果是,如何?

Amb*_*ier 7

正如文档所解释的,更新方法Repository

部分更新实体。可以通过给定条件找到实体。与 save 方法不同,它执行原始操作,不包含级联、关系和其他操作。

使用保存方法解决No entity column [...] was found

  async update(entity: Project): Promise<Project> {
    return await this.repository.save(entity);
  }
Run Code Online (Sandbox Code Playgroud)

这是使用嵌入式实体进行 OneToMany 的正确方法。

@Entity()
export class Physical extends BaseEntityModel {
  @ManyToOne(
    type => Project, project => project.context.physicalPresets,
    {onDelete: 'CASCADE'},
  )
  project: Project;
}
Run Code Online (Sandbox Code Playgroud)

另请参阅:https://github.com/typeorm/typeorm/blob/master/test/features/cascades/cascade-insert-from-both-sides/cascade-insert-from-both-sides.ts