TypeORM:在迁移中定义关系
TypeORM: Define relation in migration
您好,我正在阅读 TypeORM 文档,并尝试实现此处所示的关系
我试图拥有与每个用户相关的历史模型,以便每个用户都有多个历史
我正在读这个并使用那个例子:
https://github.com/typeorm/typeorm/blob/master/docs/many-to-one-one-to-many-relations.md
但是在尝试实现它之后,我得到了历史模型中不存在的列 userId ??
有谁知道可能是什么问题?
我假设我应该在我的模型的迁移文件中添加关系,但我在文档中没有看到任何关系?
queryRunner 有一个名为 createForeignKey
的方法。在您创建 table 的迁移文件中,它可能如下所示:
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');
}
}
您好,我正在阅读 TypeORM 文档,并尝试实现此处所示的关系 我试图拥有与每个用户相关的历史模型,以便每个用户都有多个历史
我正在读这个并使用那个例子:
https://github.com/typeorm/typeorm/blob/master/docs/many-to-one-one-to-many-relations.md
但是在尝试实现它之后,我得到了历史模型中不存在的列 userId ??
有谁知道可能是什么问题?
我假设我应该在我的模型的迁移文件中添加关系,但我在文档中没有看到任何关系?
queryRunner 有一个名为 createForeignKey
的方法。在您创建 table 的迁移文件中,它可能如下所示:
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');
}
}