我有一个用 Go 编写的 API,我有以下一组模型......type User struct { gorm.Model ID string `sql:"type:varchar(36);primary key"` Name string Password string Email string Content []Content Location string Tracks []Track Avatar string BgImg string Artists []Artist}type Artist struct { gorm.Model ID string `sql:"type:varchar(36);primary key"`}type Content struct { gorm.Model ID string `sql:"type:varchar(36);primary key"`}type Track struct { gorm.Model ID string `sql:"type:varchar(36);primary key"`}在我的main.go文件中,我正在用...初始化我的数据库drivers.DB().CreateTable(&models.User{})drivers.DB().CreateTable(&models.Artist{})drivers.DB().CreateTable(&models.Content{})drivers.DB().CreateTable(&models.Track{})但我收到错误...{ "_message": { "Severity": "ERROR", "Code": "42P01", "Message": "relation \"users\" does not exist", "Detail": "", "Hint": "", "Position": "15", "InternalPosition": "", "InternalQuery": "", "Where": "", "Schema": "", "Table": "", "Column": "", "DataTypeName": "", "Constraint": "", "File": "parse_relation.c", "Line": "1159", "Routine": "parserOpenTable" }}
1 回答
天涯尽头无女友
TA贡献1831条经验 获得超9个赞
我怀疑这是因为您的Artist,Content和Trackstruct 没有对 的父引用User,尽管User对它们有很多引用。此外,嵌入类型gorm.Model已经具有 ID 字段。
尝试:
type Artist struct {
gorm.Model
ID string `sql:"type:varchar(36);primary key"`
UserID string `sql:"type:varchar(36)"`
}
type Content struct {
gorm.Model
ID string `sql:"type:varchar(36);primary key"`
UserID string `sql:"type:varchar(36)"`
}
type Track struct {
gorm.Model
ID string `sql:"type:varchar(36);primary key"`
UserID string `sql:"type:varchar(36)"`
}
- 1 回答
- 0 关注
- 178 浏览
添加回答
举报
0/150
提交
取消