gpt4 book ai didi

postgresql - "Message": "relation\"users\"does not exist", 戈朗

转载 作者:IT王子 更新时间:2023-10-29 02:14:50 28 4
gpt4 key购买 nike

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

关于postgresql - "Message": "relation\"users\"does not exist", 戈朗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36239360/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com