我正在使用typeorm
,我知道ManyToMany
,OneToOne
等。
但我不确定在我的情况下的关系。我有一个名为Comment
的实体,以便用户可以讨论一些事情。我想添加两个名为pid
和ppid
的列。
pid
意味着当前评论的父亲,所以pid
的关系是@OneToOne
。 ppid
表示根评论。最后的表现就像下面一样
userA:xxxxx
userB reply userA:xxxxx
userC reply userB:xxxxx
userD reply userC:xxxxx
但我不确定ppid
的关系。谁能告诉我那个?
我自己有答案。
@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[];
}