更新Project
实体时出现以下错误:EntityColumnNotFound: No entity column "context.physicalPresets" was found
。
ProjectContext
嵌入Project
中。 ProjectContext
与Physical
实体具有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关系,如果可以,怎么办?
答案 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;
}