首页 > 解决方案 > ORM中一个实体的关系是什么?

问题描述

我正在使用typeorm,我知道ManyToManyOneToOne等等。

但我不确定我的情况的关系。我有一个实体命名Comment,以便用户可以讨论一些事情。我想添加 2 列名为pidand ppid

pid表示当前评论的父亲,所以 的关系pid@OneToOne。表示根ppid注释。最后的外观就像下面一样

  userA:xxxxx
    userB reply userA:xxxxx
    userC reply userB:xxxxx
    userD reply userC:xxxxx

但我不确定 的关系ppid。谁能告诉我?

标签: sqlormnestjstypeorm

解决方案


我自己有答案。

    @Entity()
    export class Comment {
        @PrimaryGeneratedColumn()
        id: number;

        @OneToOne(type => Comment)
        @JoinColumn()
        parentComment: Comment;

        @ManyToOne(type => Comment, comment => comment.comments)
        rootComment: Comment;

        @OneToMany(type => Comment, comment => comment.rootComment)
        comments: Comment[];
    }

推荐阅读