GORM Playground Link
https://github.com/go-gorm/playground/pull/808
Description
When hook tx.Statement.Changed
on func BeforeUpdate
catch "age"
create a struct, and method.
func (m *Man) update(data interface{}) error {
return DB.Set("data", data).Model(m).Where("id = ?", m.Id).Updates(data).Error
}
func (m *Man) BeforeUpdate(tx *gorm.DB) error {
if !tx.Statement.Changed("age") {
fmt.Println("no")
return nil
}
fmt.Println("yes")
return nil
}
this struct have Age
, so tx.Statement.Changed("age")
should got yes
, but got panic: reflect: Field index out of range
var change3 = struct {
Age int
}{Age: 30}
// change3 -> panic: reflect: Field index out of range
err := idx.update(change3)
if err != nil {
return
}
at playground, just change1
and change2
got yes.