Go

Go

By DeepSource

Avoid model update query with zero values in a struct GO-E1005

Bug risk
Autofix

When the model is updated with struct, GORM will only update non-zero fields. If there are fields with zero values, they are not considered.

Examples

Bad practice

// Update attributes with `struct`, will only update non-zero fields
db.Model(&user).Updates(User{Name: "hello", Age: 18, Active: false})
// UPDATE users SET name='hello', age=18, updated_at = '2013-11-17 21:34:10' WHERE id = 111;

Recommended

// Update attributes with `map`
db.Model(&user).Updates(map[string]interface{}{"name": "hello", "age": 18, "active": false})
// UPDATE users SET name='hello', age=18, active=false, updated_at='2013-11-17 21:34:10' WHERE id=111;

References