“Message”: “关系“users”不存在”,golang

huangapple go评论104阅读模式
英文:

"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"
  }
}
英文:

I have an API written in Go, I have the following set of models...

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"`
}

in my main.go file, I'm initialising my database with...

drivers.DB().CreateTable(&models.User{})
drivers.DB().CreateTable(&models.Artist{})
drivers.DB().CreateTable(&models.Content{})
drivers.DB().CreateTable(&models.Track{})

But I'm getting the error...

{
  "_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

得分: 1

我怀疑问题出在你的 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)"`
}
英文:

I suspect it is because your Artist, Content, and Track structs do not have a parent reference to the User, although the User has-many references to them. Also, the embedded type gorm.Model already has the ID field.

Try:

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)"`
}

huangapple
  • 本文由 发表于 2016年3月27日 02:46:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/36239360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定