1

我有一个用 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"
  }
}
4

1 回答 1

1

我怀疑这是因为您的Artist,ContentTrackstruct 没有对 的父引用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)"`
}
于 2016-03-26T18:56:34.163 回答