与一对多的无效关联
Invalid association with one to many
我有以下结构
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
}
type Box struct {
BoxID int `gorm:"primary_key;AUTO_INCREMENT;not null"`
StoreID int `gorm:"not null"`
Items []Item
Code int `gorm:"type:integer(13)"`
Description string `gorm:"not null"`
}
func (s *Store) AddBox(b Box) error {
err := db.Model(&s).Association("Boxes").Append(&b)
return err.Error
}
我正在 运行 测试所述结构及其功能。其中一项测试看起来像这样
func TestStoreAddBox(t *testing.T) {
b := Box{BoxID: 1}
err := b.GetDetails()
if err != nil {
t.Errorf("Expected no #1 error but got %v", err)
}
s := Store{StoreID: 2}
err = s.GetDetails()
if err != nil {
t.Errorf("Expected no #2 error but got %v", err)
}
err = s.AddBox(b)
if err != nil {
t.Errorf("Expected no #3 error but got %v", err)
}
}
现在,如果我开始测试,我会收到以下错误:
--- FAIL: TestStoreAddBox (0.00s)
db100_test.go:371: Expected no #3 error but got invalid association Boxes for db100.Store
有人知道这里的问题是什么吗?
好吧,GORM 文档中的关键是下面这句话:
To define a has many relatinship, foreign key must exists, default foreign key’s name is owner’s type name plus its primary key.
所以在我的情况下会是
StoreStoreID
所以我不得不更改 Store 结构 lice 所以
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 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
}
type Box struct {
BoxID int `gorm:"primary_key;AUTO_INCREMENT;not null"`
StoreID int `gorm:"not null"`
Items []Item
Code int `gorm:"type:integer(13)"`
Description string `gorm:"not null"`
}
func (s *Store) AddBox(b Box) error {
err := db.Model(&s).Association("Boxes").Append(&b)
return err.Error
}
我正在 运行 测试所述结构及其功能。其中一项测试看起来像这样
func TestStoreAddBox(t *testing.T) {
b := Box{BoxID: 1}
err := b.GetDetails()
if err != nil {
t.Errorf("Expected no #1 error but got %v", err)
}
s := Store{StoreID: 2}
err = s.GetDetails()
if err != nil {
t.Errorf("Expected no #2 error but got %v", err)
}
err = s.AddBox(b)
if err != nil {
t.Errorf("Expected no #3 error but got %v", err)
}
}
现在,如果我开始测试,我会收到以下错误:
--- FAIL: TestStoreAddBox (0.00s)
db100_test.go:371: Expected no #3 error but got invalid association Boxes for db100.Store
有人知道这里的问题是什么吗?
好吧,GORM 文档中的关键是下面这句话:
To define a has many relatinship, foreign key must exists, default foreign key’s name is owner’s type name plus its primary key.
所以在我的情况下会是
StoreStoreID
所以我不得不更改 Store 结构 lice 所以
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"`
}