英文:
"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
我怀疑问题出在你的 Artist
、Content
和 Track
结构体上,它们没有一个指向 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)"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论