繁体   English   中英

Typeorm Migration 用于自引用迁移

[英]Typeorm Migration for self referencing migration

我正在尝试为自引用模型创建迁移,如 TypeOrm 文档中所述

https://github.com/typeorm/typeorm/blob/master/docs/relations-faq.md#how-to-create-self-referencing-relation

但是,如何在此处为这些自引用关系创建迁移:

我得到了模型:

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',
            }),
        );
}

我认为没问题,但是如何为 Child 类别创建迁移中的外键,因为它应该是一个类别数组???

伙计,我遇到了同样的错误,我不知道它是否会帮助你,但在你的专栏中

 { name: 'parentId', type: 'int' },

为此改变

   { name: 'parentId', type: 'int' , unsigned: true},

暂无
暂无

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

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