node.js - 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".
...
}
解决方案
我在我的实体中以这种方式使用它:
@Entity()
export class Issue {
@ManyToOne(type => Project, project => project.issues)
project: Project
@RelationId((issue: Issue) => issue.project)
projectId: number;
...
}
它工作正常,但不确定它是否是最佳实践。希望能帮助到你。
推荐阅读
- python - Python tkinter 编码器,但是程序太长了
- javascript - JavaScript中以秒为单位的日期减法
- sql - Postgresql - 唯一约束只允许一个空
- r - 包含 120 个数据帧 (11.1 GB) 的保存列表
- javascript - 使用 responsiveVoice api 的 Javascript 数组
- .net-core - 如何在 dotnet 核心 Web 项目(在 IIS Express 中运行)中跟踪失败的程序集绑定
- azure - 可以使用 CLI、ARM 或 Powershell 将 Defender 添加到 Azure Sql 数据库吗?
- ios - 出口合规信息。您的应用程序使用加密吗?在测试飞行中
- telegram - 如何扩展基于 aiogram 的 Telegram 机器人?
- php - 如何将数组保存在 RAM 中并搜索条目