Skip to content

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

Closed
nibazshab wants to merge 1 commit intogo-gorm:masterfrom
nibazshab:master
Closed

Statement.Changed can't work on a new custom struct#808
nibazshab wants to merge 1 commit intogo-gorm:masterfrom
nibazshab:master

Conversation

@nibazshab
Copy link
Copy Markdown

Explain your user case and expected results

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
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant