Go Gorm

Go Gorm

gorm

gorm官网

gorm开始

Delete

https://gorm.io/docs/delete.html db.Unscoped().Where().Delete(&) 硬删除 db.Where().Delete() 软删除

Update

如果一个值是空,怎么避免更新已经有的值 NOTE 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 从官文的解释可以看出,如果避免空值替换,就用struct,否则用map或者select

但是这里涉及到一个问题 比如一个主机,可能有Description,也可能没有,在更新表单中 这个时候传参不建议用map了,建议用对应的model,如果觉得不方便,还是想用map 那可能需要判断是否为空值进行delete操作

应该还有其他方法,比如在struct里面定义啥的,目前我还没有找到更好的办法