自引用的话比较简单 ,这一篇就是用来水的

书接上文,现在需要实现评论的评论,怎么办?

很简单,只需这样

1
2
3
4
5
6
7
8
9
10
11
12
13
type Comment struct {
Id int
Body string
Comments []Comment `gorm:"polymorphic:Owner;"`

OwnerID int // 拥有者ID
OwnerType string // 拥有者类型
}

// 添加评论
func AddComment(Body string, OwnerID int, OwnerType string) {
GLOBAL_DB.Create(&Comment{Body: Body, OwnerID: OwnerID, OwnerType: OwnerType})
}