提问人:Ash Smith 提问时间:11/17/2023 更新时间:11/17/2023 访问量:23
gorm:无法预加载 main 的字段存储库。应用
gorm: can't preload field Repository for main.Application
问:
当到达此端点时,我收到以下错误,然后是进程退出:
2023/11/16 20:13:38 can't preload field Repository for main.Application
exit status 1
我正在尝试预加载存储库,以便我可以在前端访问存储库的 URL
路由处理程序:
func ApplicationsHandler(w http.ResponseWriter, r *http.Request) {
var applications []Application
var queryError = database.Model(Application{}).
Preload("Repository").
Find(&applications).
Error
if queryError != nil {
log.Fatalln(queryError)
}
json.NewEncoder(w).Encode(applications)
}
结构:
type Application struct {
ID int64 `json:"id" gorm:"primary_key"`
Name string `json:"name"`
Repository ApplicationRepository `json:"repository_id" gorm:"ForeignKey:repository_id"`
Username string `json:"username"`
IpAddress string `json:"ip_addreess"`
}
type ApplicationRepository struct {
ID int64 `json:"id" gorm:"primary_key"`
Url string `json:"url"`
}
答:
0赞
Trock
11/17/2023
#1
您需要为关系添加内容。ApplicationID
ApplicationRepository
type Application struct {
ID int64 `json:"id" gorm:"primary_key"`
Name string `json:"name"`
Repository ApplicationRepository `json:"repository"`
Username string `json:"username"`
IpAddress string `json:"ip_addreess"`
}
type ApplicationRepository struct {
ID int64 `json:"id" gorm:"primary_key"`
Url string `json:"url"`
ApplicationID `json:"application_id"`
}
评论