gpt4 book ai didi

node.js - Typeorm Migration 用于自引用迁移

转载 作者:行者123 更新时间:2023-12-03 22:25:59 25 4
gpt4 key购买 nike

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

关于node.js - Typeorm Migration 用于自引用迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60525525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com