我有以下结构: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")}我不确定我做错了什么。
- 1 回答
- 0 关注
- 152 浏览
添加回答
举报
0/150
提交
取消