【发布时间】:2020-12-23 21:31:48
【问题描述】:
我正在尝试自定义 many2many 表连接。我有两个表,我想从中获取 id 并想要另一个字段,这将告诉我何时创建连接表中的条目。 id 正常,但“created_at”没有更新并显示“Null”而不是时间。
// this is the table join struct which I want to make
type UserChallenges struct {
gorm.JoinTableHandler
CreatedAt time.Time
UserID int
ChallengeID int
}
//hook before create
func (UserChallenges) BeforeCreate(Db \*gorm.DB) error {
Db.SetJoinTableHandler(&User{}, "ChallengeId", &UserChallenges{})
return nil
}
这不会对构建产生任何错误。请告诉我我缺少什么,以便我可以在其中获取创建时间字段。
PS - gorm.io 上的 GORM 文档仍然显示 SetupJoinTable 方法,但在较新版本中已弃用。有一个 SetJoinTableHandler,但在任何地方都没有可用的文档。
【问题讨论】:
-
SetJoinTableHandler 似乎来自早期版本:github.com/go-gorm/gorm/… 在当前代码中未显示名称。