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
}
posted @ 2022-07-06 20:02  vx_guanchaoguo0  阅读(360)  评论(0编辑  收藏  举报