ORM中一个实体的关系是什么?

问题描述 投票:-1回答:1

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

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

pid意味着当前评论的父亲,所以pid的关系是@OneToOneppid表示根评论。最后的表现就像下面一样

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

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

sql orm nestjs typeorm
1个回答
0
投票

我自己有答案。

    @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[];
    }
© www.soinside.com 2019 - 2024. All rights reserved.