先说解决方法,采用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删除,避免间隙锁。