首页 > 解决方案 > TypeORM 迁移将 id 列绑定到对象

问题描述

我在 postgres 中创建了 2 个表 - 问题和项目。每个问题都来自一个项目,因此问题表中存在一个项目 ID 外键。在运行“typeorm-model-generator”而不是在类型编号问题中获取 projectId 字段后,我得到了 project 类型的 projectId。我在这里错过了一个设置吗?

数据库:postgres TypeORM 版本:0.1.12

@entity('Issue')
export class Issue {

@ManyToOne(type => Project, projectId => projectId.issues)
@JoinColumn({ name: 'projectId'})
projectId: Project; // this should be of type number and a new field called project should be created with type "project". 

...

}

标签: node.jstypeorm

解决方案


我在我的实体中以这种方式使用它:

@Entity()
export class Issue {

    @ManyToOne(type => Project, project => project.issues)
    project: Project

    @RelationId((issue: Issue) => issue.project)
    projectId: number;

    ...
}

它工作正常,但不确定它是否是最佳实践。希望能帮助到你。


推荐阅读