gorm 强制更新零值字段

本文探讨了在Go语言中使用Gorm库更新数据库记录时遇到的问题,即零值字段未被更新。作者分享了两种解决方法:1) 使用Map传值,确保所有字段明确设置;2) 尝试使用`force`标签,但未成功。最后,建议使用`Save`方法,注意在更新前先删除原有记录以避免唯一索引冲突。该文对于使用Gorm进行数据库操作的开发者具有参考价值。
摘要由CSDN通过智能技术生成

先说解决方法,采用map传值

err := getReplenishmentRunWaveDefaultSettingSqlCommon(ctx).Where("id = ?", model.ID).Updates(map[string]interface{}{
		"channel_id_list": ent.ChannelIdList,
		"repl_type": ent.ReplType,
		"contains_oos": ent.ContainsOos,
		"contains_non_oos": ent.ContainsNonOos,
		"is_urgent_order_only": ent.IsUrgentOrderOnly,
		"sku_sales_type": ent.SkuSalesType,
		"sku_abc_level_list": ent.SkuAbcLevelList,
		"category_id_l1_list": ent.CategoryIdL1List,
		"covered_zones_list": ent.CoveredZonesList,
		"max_rt_order_per_wave": ent.MaxRtOrderPerWave,
		"max_qty_of_from_location_per_rt": ent.MaxQtyOfFromLocationPerRt,
		"rack_transfer_type": ent.RackTransferType,
		"mtime": ent.Mtime,
	}).GetError()

执行的SQL为

UPDATE `replenishment_run_wave_default_setting_tab` SET `category_id_l1_list` = '', `channel_id_list` = '', `contains_non_oos` = '0', `contains_oos` = '1', `covered_zones_list` = '', `is_urgent_order_only` = '1', `max_qty_of_from_location_per_rt` = '0', `max_rt_order_per_wave` = '0', `mtime` = '1637052798', `rack_transfer_type` = '2', `repl_type` = '6', `sku_abc_level_list` = '', `sku_sales_type` = '0'  WHERE (id = '1') 

之前没传零值,是采用的结构体传值

type ReplenishmentRunWaveDefaultSettingEntity struct {
	ID                        int64  `gorm:"column:id;primary_key" json:"id"`
	WhsID                     string `gorm:"column:whs_id" json:"whs_id"`
	Operator                  string `gorm:"column:operator" json:"operator"`
	ChannelIdList             string `gorm:"column:channel_id_list" json:"channel_id_list"`
	ReplType                  int64  `gorm:"column:repl_type" json:"repl_type"`
	ContainsOos               int64  `gorm:"column:contains_oos" json:"contains_oos"`
	ContainsNonOos            int64  `gorm:"column:contains_non_oos" json:"contains_non_oos"`
	IsUrgentOrderOnly         int64  `gorm:"column:is_urgent_order_only" json:"is_urgent_order_only"`
	SkuSalesType              int64  `gorm:"column:sku_sales_type" json:"sku_sales_type"`
	SkuAbcLevelList           string `gorm:"column:sku_abc_level_list" json:"sku_abc_level_list"`
	CategoryIdL1List          string `gorm:"column:category_id_l1_list" json:"category_id_l1_list"`
	CoveredZonesList          string `gorm:"column:covered_zones_list" json:"covered_zones_list"`
	MaxRtOrderPerWave         int64  `gorm:"column:max_rt_order_per_wave" json:"max_rt_order_per_wave"`
	MaxQtyOfFromLocationPerRt int64  `gorm:"column:max_qty_of_from_location_per_rt" json:"max_qty_of_from_location_per_rt"`
	RackTransferType          int64  `gorm:"column:rack_transfer_type" json:"rack_transfer_type"`
	Ctime                     int64  `gorm:"column:ctime" json:"ctime"`
	Mtime                     int64  `gorm:"column:mtime" json:"mtime"`
}

err := getReplenishmentRunWaveDefaultSettingSqlCommon(ctx).Where("id = ?", model.ID).Updates(ent).GetError()

执行的SQL为

UPDATE `replenishment_run_wave_default_setting_tab` SET `contains_oos` = '1', `is_urgent_order_only` = '1', `mtime` = '1637053293', `operator` = 'xxx', `rack_transfer_type` = '2', `repl_type` = '6', `whs_id` = 'xxx'  WHERE (id = '1') 

忽略了零值

采用其他解决方法

解决方法一

用force标签,例如

CategoryIdL1List          string `gorm:"force;column:category_id_l1_list" json:"category_id_l1_list"`

没效果,可能是写法不对

解决方法二

使用Save方法
执行SQL时采用的是Insert,如果有唯一索引限制就插入失败,所以使用时先删后插入可以解决。删除时最好先查,找出主键id,再用主键Id删除,避免间隙锁。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值