"Message": "relation \"users\"不存在", golang

"Message": "relation \"users\" does not exist", golang

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

我怀疑这是因为您的 ArtistContentTrack 结构没有对 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)"`
}