TypeOrm树迁移解决方案

时间:2020-03-05 13:00:33

标签: node.js postgresql typeorm

我正在尝试在TypeORM中创建Tree结构,现在我正在尝试添加迁移文件

这是我的树结构:

import {
    Entity,
    Column,
    PrimaryGeneratedColumn,
    OneToMany,
    CreateDateColumn,
    Tree,
    TreeChildren,
    TreeParent,
} from 'typeorm';

@Entity('tree')
@Tree('materialized-path')
export class Tree {
    @PrimaryGeneratedColumn({ name: 'id', type: 'integer' })
    id: number;


    @Column()
    name: string;


    @CreateDateColumn({ type: 'timestamptz' })
    createdAt: string;

    @TreeChildren()
    children: AssetType[];

    @TreeParent()
    parent: AssetType;

}

我的问题是现在应该如何为从TypeORM导入的该树创建迁移文件?

1 个答案:

答案 0 :(得分:0)

我使用TypeORM Cli进行迁移

使用的命令:typeorm迁移:生成如下所述:

https://github.com/typeorm/typeorm/blob/master/docs/migrations.md#generating-migrations

它是在迁移后创建的:

await queryRunner.query(`CREATE TABLE "table_name" ("id" SERIAL NOT NULL, "name" character varying NOT NULL,"mpath" character varying DEFAULT '', "parentId" integer, CONSTRAINT "PK_9b5ee2748943131ed9d9831e8c9" PRIMARY KEY ("id"))`, undefined);
await queryRunner.query(`ALTER TABLE "table_name" ADD CONSTRAINT "FK_9da62161e3c7bc51981ff7fa1da" FOREIGN KEY ("parentId") REFERENCES "table_name"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, undefined)

;