node.js - TypeORM:定义迁移中的关系
问题描述
嗨,我正在阅读 TypeORM 文档,并尝试实现此处所示的关系 我试图拥有与每个用户相关的历史模型,以便每个用户都有多个历史记录
我正在阅读并使用该示例:
https://github.com/typeorm/typeorm/blob/master/docs/many-to-one-one-to-many-relations.md
但是在尝试实现它之后,我得到历史模型上的 userId 列不存在?
有谁知道可能是什么问题?
我假设我应该在我的模型的迁移文件中添加关系,但我在文档中没有看到任何内容?
解决方案
queryRunner 有一个名为createForeignKey
. 在您创建表的迁移文件中,它可能如下所示:
export class ExampleMigration implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.createTable(
new Table({
name: 'stuff',
columns: [
{
name: 'id',
type: 'uuid',
isPrimary: true
},
{
name: 'userId',
type: 'uuid'
}
]
})
);
await queryRunner.createForeignKey(
'stuff',
new TableForeignKey({
columnNames: ['userId'],
referencedTableName: 'users',
referencedColumnNames: ['id']
})
);
}
public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.dropTable('userSessions');
}
}
推荐阅读
- arrays - MongoDB 根据之前的元素删除元素(迭代)
- swift - 如何重写代码以摆脱 Swift 中的重复代码?
- android - “在 [location] 找到的 adb 二进制文件已过时并且存在严重的性能问题”但模拟器运行!显示消息后
- ios - 当应用程序在 iOS 中处于非活动状态时搜索蓝牙/BLE 设备?
- node.js - 在 FireBase 上部署 Kahoot 克隆
- git - 是否可以将整个 git commit 历史记录下载为列表?
- python - 通过多个列表并行迭代,并在迭代项上建立索引
- sqlite - 如何在 ALTER 查询之前检查 TABLE 是否存在?
- python - 如何在 S3 中的 Pyspark 中使用包含子文件夹的文件夹中的 CSV 文件创建新的数据框
- windows - 如何从 startActivity (intent) 的内置 UI 中删除取消按钮,