【问题标题】:What's relation of one entity relate himself in ORM?ORM中一个实体的关系是什么?
【发布时间】:2019-08-13 14:58:56
【问题描述】:

我用的是typeorm,我知道ManyToManyOneToOne等等。

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

pid 表示当前评论的父亲,所以pid 的关系是@OneToOneppid 表示根注释。最终的外观如下所示

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

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

【问题讨论】:

  • 可以通过父母的递归调用(pids)访问根评论。你可以考虑忽略它。

标签: sql orm nestjs typeorm


【解决方案1】:

我自己有答案。

    @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[];
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2011-01-28
    • 2015-03-31
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多