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

时间:2019-02-27 11:23:28

标签: typeorm

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

如此处https://typeorm.io/#/embedded-entities所述,

ProjectContext嵌入Project中。 ProjectContextPhysical实体具有OneToMany关系。

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

在以下方面没有更多的成功

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

我得到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);
  }

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

1 个答案:

答案 0 :(得分:1)

如文档所述,Repository的更新方法:

  

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

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

  async update(entity: Project): Promise<Project> {
    return await this.repository.save(entity);
  }

这是对嵌入式实体执行OneToMany的正确方法。

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

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