一七零、GORM值为0或者空字符串的时候不能被更新&创建的五种解决办法

在使用grom时,如果用结构体的形式,对数据库进行更新时,会出现一个问题:当我们想要将某个字段的值更新为0&空字符串,执行update之后会发现该数据实际上并没有被更新。

例如有如下数据表:

id name score desc
1 王五 99 调皮

有如下结构体:

复制代码
type Student struct{
	Id    int32 `gorm:"type:int(11);column:id;primaryKey;autoIncrement;comment:" json:"id"`
	Name  string `gorm:"type:varchar(100);column:name;not null;default:'';comment:姓名"`
	Score int32 `gorm:"type:int(11);column:score;comment:分数"`
	Desc  string `gorm:"type:varchar(1000);column:desc;default:'';comment:备注"`
}

当我们执行以下操作后

复制代码
upStudent := Student{
	Id    :1,
	Name  :张三,
	Score :0,
	Desc:""
}

db.Model(&student).Where("id=?", Student.Id).Updates(Student)

会发现,执行成功之后,score依旧等于99,Desc也是"调皮"

解决方法一:结合 Select 和 Omit

复制代码
fields := []string{"id","name","score","desc"}
db.Model(&Student).Select(fields).Where("id=?", Student.Id).Updates(Student)
// 或者Omit
db.Model(&Student{}).Omit("id").Where("id = ?", upStudent.Id).Updates(upStudent)

解决方法二:使用Save

注:Save 会保存所有的字段,即使字段是零值;保存是一个组合函数。 如果保存值不包含主键,它将执行 Create,否则它将执行 Update (包含所有字段)。

复制代码
db.Model(&Student).Where("id=?", Student.Id).Save(Student)

解决方法三:使用map接口,即mapstringinterface{} (推荐)

注:我们使用的是protobuf定义了的结构时,转换成map有些许麻烦。

复制代码
values := map[string]interface{}{
				"id":          upStudent.Id,
				"name":      upStudent.Name,
				"score":   upStudent,Score,
				"desc":upStudent,Desc,
		}
db.Model(&Student).Where("id=?", Student.Id).Updates(values)

解决方法三:使用map接口,即mapstringinterface{} (推荐)

注:我们使用的是protobuf定义了的结构时,转换成map有些许麻烦。

复制代码
values := map[string]interface{}{
				"id":          upStudent.Id,
				"name":      upStudent.Name,
				"score":   upStudent,Score,
				"desc":upStudent,Desc,
		}
db.Model(&Student).Where("id=?", Student.Id).Updates(values)

解决方法四:逐字段单独赋值

注:我们使用的是protobuf定义了的结构时,转换成map有些许麻烦。

复制代码
upStudent := Student{
	Id    : 1,
}

db.Model(&Student{}).Where("id = ?", upStudent.Id).Update("score", 0).Update("name", "张三").Update("desc", "")

解决方法五:定义 Struct 时将字段设置为指针类型

通过将字段定义为指针类型,可以避免默认忽略 0 值的问题。同样适用于空字符串。

复制代码
score := int32(0)
name := "张三"
desc := ""
upStudent := Student{
	Id    : 1,
	Name  : &name,
	Score : &score,
	Desc: &desc,
}
db.Model(&Student{}).Where("id = ?", upStudent.Id).Updates(upStudent)
相关推荐
吠品16 小时前
Zabbix Web界面误报Server未运行的排查与解决
java·服务器·数据库
XMAIPC_Robot16 小时前
软硬协同实时控制|RK3588业务调度+FPGA硬件时序,ethercat实现半导体设备微秒级响应(125us)
linux·arm开发·人工智能·fpga开发
SelectDB16 小时前
Apache Doris Variant 动态 JSON 实战教程:从建表到跑通 100M Agent 日志查询
数据库
重生的黑客16 小时前
Linux 进程优先级、切换与调度:从孤儿进程到 O(1) 调度模型
linux·运维·服务器·进程优先级·nice
sg_knight17 小时前
MySQL 存储过程详解:从入门到实战
android·数据库·mysql·database·dba·关系型数据库·db
星空露珠17 小时前
28种颜色对应名称,
开发语言·数据库·算法·游戏·lua
Java开发追求者18 小时前
Go环境开发配置
go·go环境开发配置·环境开发配置
骑上单车去旅行18 小时前
MD5校验对比脚本
linux·服务器·windows
平生幻18 小时前
Linux 常用命令
linux
踏月的造梦星球18 小时前
DM8 DSC 单机双实例部署
运维·开发语言·数据库