sql - ORM中一个实体的关系是什么?
问题描述
我正在使用typeorm
,我知道ManyToMany
,OneToOne
等等。
但我不确定我的情况的关系。我有一个实体命名Comment
,以便用户可以讨论一些事情。我想添加 2 列名为pid
and 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[];
}
推荐阅读
- node.js - 嵌套函数的打字稿定义
- javascript - Paypal 更改地址字段样式
- html - 盒子没有在底部排列
- c++ - C++:不同类型的容器
- eclipse - 无法将 Spark 与 Eclipse 中的 Hortonworks Sandbox 连接
- image - 颤振不会在网络中显示图像
- android - 我无法从可绘制文件夹中插入图标
- python - 获取导致测试发现在 VSCode 中失败的 TypeError 的 Linenumber
- html - 图像在 chrome 和 firefox 上显示不同,使用 flexbox 调整大小
- swift - macOS Bundles 之间的双向数据交换 (swift)