繁体   English   中英

TypeORM:在迁移中定义关系

[英]TypeORM: Define relation in migration

嗨,我正在阅读 TypeORM 文档,并试图实现像这里显示的关系我试图拥有与每个用户相关的历史模型,以便每个用户都有多个历史记录

我正在阅读并使用该示例:

https://github.com/typeorm/typeorm/blob/master/docs/many-to-one-one-to-many-relations.md

但是在尝试实现它之后,我得到了 History 模型上的列 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');
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM