gorm plugin for allow update zero value field.
When update with struct, GORM will only update non-zero fields, you might want to use map to update attributes or use Select to specify fields to update Updates-multiple-columns
This works in most cases, but there are times when we just want to allow individual 0 values to be updated, and neither map[string]interface
nor Select
is very friendly to us.
-
NewPlugin
register plugin togorm.DB
db.Use(zerofield.NewPlugin())
-
UpdateScopes
update event it's zero field// ... user.Name = "" user.Age = 0 user.Active = false user.Birthday = nil // will always update Name,Age even if it's zero field // Active,Birthday will not be saved db.Scopes(zerofield.UpdateScopes("Name","Age")).Updates(&user) // if cloumns is empty, all field will be save like db.Select("*"") db.Scopes(zerofield.UpdateScopes()).Updates(&user)