Gorm 更新数据默认值不更新问题
有个需求需要将用户分数更新为0分 然鹅grom 是0值不更新
- 找了一大圈说是用map 代替struct update 但是依然失败
方式1
type Temp struct{
Int32 sql.NullInt32
Bool bool
}
sql.NullInt32{Int32: 0, Valid: true}
方式二
data := make(map[string]interface{})
data["stock"] = 0 //零值字段
data["price"] = 35
// 等价于
UPDATE `foods` SET `price` = '35', `stock` = '0' WHERE (id = '2')
db.Model(Food{}).Where("id = ?", 2).Updates(data)
方式三
type Temp struct{
String string
Bool bool
}
// 修改为指针
type Temp struct{
String *string
Bool *bool
}
本文来自博客园,作者:vx_guanchaoguo0,转载请注明原文链接:https://www.cnblogs.com/guanchaoguo/p/16452327.html

浙公网安备 33010602011771号