我正在尝试为自引用模型创建迁移,如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',
}),
);
}
我认为还可以,但是我应该如何在迁移中创建子类别的外键,因为它应该是类别的数组?
答案 0 :(得分:1)
伙计,我遇到了同样的错误,我不知道它是否会帮助你,但在你的专栏中
{ name: 'parentId', type: 'int' },
为此而改变
{ name: 'parentId', type: 'int' , unsigned: true},