typeorm - TypeORM - 一对多:EntityColumnNotFound:未找到实体列“context.physicalPresets”
问题描述
Project
更新实体时出现以下错误: EntityColumnNotFound: No entity column "context.physicalPresets" was found
.
ProjectContext
Project
如此处所述嵌入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 关系,如果可以,如何?
解决方案
正如文档所解释的,更新方法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;
}
推荐阅读
- c# - 如何保持客户端套接字连接打开并在控制台应用程序中发送/接收多条消息
- javascript - 如何在重置其他两个的文本时更改选中的单选按钮元素上的文本?
- javascript - 请求时如何使用 Multer 上传单个图像。使用 Postman 测试时文件总是空的?
- mongodb - 如何在 Mongodb 的同一查询中使用 dept 和 $arrayElemAt?
- sql - Google BigQuery 根据开始/结束日期按日期确定历史“活动”状态
- javascript - 使用 JavaScript 或 Jquery 创建模式
- linux - 错误:在安装avog2s的make过程中ld返回1退出状态
- flutter - 当应用程序以颤振退出时,重复通知不起作用
- python - SELECT EXISTS 检查行是否存在于另一个表中
- save - 如何在 Mathematica 中修复此错误:位置 1 的参数应该是机器大小的实数?