Go

Go

By DeepSource

Avoid building query with zero values in a struct GO-E1004

Bug risk
Autofix

In GORM, queries are built with non-zero fields of the struct. If there are fields with zero values, they are not considered and not included in query conditions.

Examples

Bad practice

// Age is of type `int` and `0` is the zero value for `int`.
db.Where(&User{Name: "jinzhu", Age: 0}).First(&users)

Recommended

db.Where(map[string]interface{}{"name": "jinzhu", "age": 0}).First(&users)

References