【发布时间】:2020-06-16 21:56:25
【问题描述】:
我正在尝试为自引用模型创建迁移,如 TypeOrm 文档中所述
但是我如何在此处为这些自引用关系创建迁移:
我有模特:
import {Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToMany} from "typeorm";
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
text: string;
@ManyToOne(type => Category, category => category.childCategories)
parentCategory: Category;
@OneToMany(type => Category, category => category.parentCategory)
childCategories: Category[];
}
我的迁移看起来像这样:
export class createCategoryTable1576071180569 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.createTable(
new Table({
name: 'category',
columns: [
{ name: 'id', type: 'int', isPrimary: true, isGenerated: true, generationStrategy: 'increment' },
{ name: 'text', type: 'varchar' },
{ name: 'parentId', type: 'int' },
{ name: 'childId', type: 'int' },
],
}),
true,
);
await queryRunner.createForeignKey(
'category',
new TableForeignKey({
columnNames: ['parentId'],
referencedColumnNames: ['id'],
referencedTableName: 'category',
onDelete: 'CASCADE',
}),
);
}
我认为没问题,但是我如何在迁移中为子类别创建外键,因为它应该是一个类别数组???
【问题讨论】:
标签: node.js sequelize.js typeorm