Skip to content

Statement.Changed can't work on a new custom struct #7486

@nibazshab

Description

@nibazshab

GORM Playground Link

go-gorm/playground#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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions