I'm trying to update an existing record in PostgreSQL database. I have a column which accepts integers. I have tried doing the update using db query written in gorm. All the integers are getting updated except 0
(zero).
Can anyone help me fix this issue?
type RoleTypes struct {
ID int `gorm:"primary_key,AUTO_INCREMENT"`
RoleName string
IsEnable int
}
Below is the db query:
func UpdateRoles(db *gorm.DB, r *models.RoleTypes) (err error) {
Logr.Debug("Inside UpdateRoles method")
if err = db.Debug().Model(&r).Where("ID=?", r.ID).Update(&r).Error; err != nil {
Logr.Error("error occured!!", zap.Any("Error", err))
return err
}
return nil
}
I'm trying to Update the IsEnable
to 0
(Zero).
IsEnable
is actually aboolean
in the database? (and if not: why not? aboolean
would be the natural choice for a true/false flag)gorm:"default:0"
. (doc.gorm.io/crud.html#default-values) "NOTE all fields having zero value, like 0, '', false or other zero values won't be saved into database but will use its default value, it you want to avoid this, consider to use pointer type or scaner/valuer"