我有以下结构:
type Player struct {
ID uint `gorm:"primarykey"`
ClubID uint
Club Club
}
type Club struct {
ID uint `gorm:"primarykey"`
Name string `gorm:"unique"`
}
我正在从文件中加载此信息,例如:
func TestSQLite(t *testing.T) {
pr, err := db.Init("file:memdb1?mode=memory&cache=shared")
if err != nil {
fmt.Println("Error on Init: " + err.Error())
t.Fail()
}
p := domain.Player{
Club: domain.Club{Name: "Hello"},
Shooting: 23,
PlayerPositions: []domain.Position{
{
Name: "SM",
},
},
}
if err := pr.Create(&p); err != nil {
fmt.Println("Error creating player: " + err.Error())
t.Fail()
}
c := domain.Player{
Club: domain.Club{Name: "Hello"},
Shooting: 23,
PlayerPositions: []domain.Position{
{
Name: "SM",
},
},
}
if err := pr.Create(&c); err != nil {
fmt.Println("Error creating player: " + err.Error())
t.Fail()
}
t.Logf("%d", p.ClubID)
t.Logf("%d", c.ClubID)
if c.ClubID != p.ClubID {
t.Fail()
}
}
结果我有:
db_test.go:45: 1
db_test.go:46: 0
创建代码是
return pr.preload().Debug().Create(&p).Error
和预载是
func (pr *PlayerRepository) preload() *gorm.DB {
return pr.connection.
Preload("Club")
}
我不确定我做错了什么。
精慕HU
相关分类