2019/12/18

golang gorm update when record not exist

when record is not exist. update will be success.
have to check RowsAffected .
the same as delte

query := db.Model(r).Updates(something)
if query.Error != nil {
   return query.Error
}
if query.RowsAffected == 0 {
   return gorm.ErrRecordNotFound
}



query := db.Delete(&something{ID: ID})
if query.Error != nil {
   return query.Error
}
if query.RowsAffected == 0 {
   return gorm.ErrRecordNotFound
}