Your Question
I have a big table which have many columns, each time I update some fileds and call Save(), all fields are updated event their value are not changed.
In some java orm, they do only update the changed fields, is there any way to so in gorm ? I see something as follows, if we can know if one column has been changed and we know all columns, thus we can achieve it technically.
if tx.Statement.Changed("Role") {
return errors.New("role not allowed to change")
}
The document you expected this should be explained
if this is already supported by gorm
Expected answer
Save() only updates fields that are changed.
Comment From: ghost
hello, you can use updates to solve it. https://gorm.io/zh_CN/docs/update.html#%E6%9B%B4%E6%96%B0%E5%A4%9A%E5%88%97
Comment From: seaguest
@longlihale I know, I just don't want to manually find all unchanged fields, actually I marshal old, new model and compare to extract the different map, then call Updates(). But I think it would be great if gorm support this feature inside.
Comment From: jinzhu
We can't do this automatically due to golang's limitation, maybe you can write a wrapper to check the changes and only update those changed.