Go 选择正确的方法
我正试着做一个仓库的模型。现在我有了我的东西设置,知道如何建模,但我不知道如何用gorm正确地选择这里的东西 我有以下几种:Go 选择正确的方法,go,associations,go-gorm,Go,Associations,Go Gorm,我正试着做一个仓库的模型。现在我有了我的东西设置,知道如何建模,但我不知道如何用gorm正确地选择这里的东西 我有以下几种: type Store struct { StoreID int `gorm:"primary_key;AUTO_INCREMENT;not null"` Name string `gorm:"not null"` Adress string `gorm:"not null"` Manager User `g
type Store struct {
StoreID int `gorm:"primary_key;AUTO_INCREMENT;not null"`
Name string `gorm:"not null"`
Adress string `gorm:"not null"`
Manager User `gorm:"not null"`
ManagerID int `gorm:"foreignkey:ManagerID;not null"`
Boxes []Box `gorm:"foreignkey:StoreID;association_foreignkey:StoreID"`
}
type User struct {
UserID int `json:"id" gorm:"primary_key;AUTO_INCREMENT;not null"`
Username string `json:"username" gorm:"not null"`
Password string `json:"password" gorm:"not null"`
Email string `json:"email" gorm:"not null"`
Right UserRight `json:"userright" gorm:"not null"`
}
type Box struct {
BoxID int `gorm:"primary_key;AUTO_INCREMENT;not null"`
StoreID int `gorm:"not null"`
Code int `gorm:"type:integer(13)"`
Description string `gorm:"not null"`
}
现在,我想选择所有的盒子,包括它们的相关商店以及这些商店的相关经理。
我是否必须使用联接来完成此操作,或者gorm有更好的方法来完成此操作?试试看
boxes := []Box{}
db.Model(&Box{}).Preload("Store.Manager").Find(&boxes)
请看一看,我多次阅读了整个文档,相信我,我没有做到这一点,否则我就不会要求设置调试选项来查看生成的查询。这看起来像一个dup()。希望帮助不是我现在唯一能得到的是
错误获取框:无法预加载db100.Box的字段存储id
。此外,我不认为这是配音,因为我想加载的是父母,而不是孩子。我想得到他们所在的商店以及商店经理的所有箱子的清单。