【问题标题】:Typeorm Migration for self referencing migration用于自引用迁移的 Typeorm 迁移
【发布时间】:2020-06-16 21:56:25
【问题描述】:

我正在尝试为自引用模型创建迁移,如 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',
            }),
        );
}

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

【问题讨论】:

    标签: node.js sequelize.js typeorm


    【解决方案1】:

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

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

    为此改变

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-23
      • 2021-05-23
      • 2020-06-18
      • 2019-11-17
      • 1970-01-01
      • 2023-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多