首页 > 解决方案 > TypeORM - 一对多:EntityColumnNotFound:未找到实体列“context.physicalPresets”

问题描述

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

ProjectContextProject如此处所述嵌入https://typeorm.io/#/embedded-entities。与实体ProjectContext具有 OneToMany 关系。Physical

@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 关系,如果可以,如何?

标签: typeorm

解决方案


正如文档所解释的,更新方法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


推荐阅读