gen
gen copied to clipboard
使用updates传入struct中int64无法更新为0,使用interface可以正常更新数据
u.WithContext(ctx).Where(u.ID.Eq(111)).Updates(map[string]interface{}{"name": "hello", "age": 18, "active": false}) 更新正常u.WithContext(ctx).Where(u.ID.Eq(111)).Updates(map[string]interface{}{"name": "hello", "age": 18, "active": false}) 更新正常
u.WithContext(ctx).Where(u.ID.Eq(111)).Updates(model.User{Name: "hello", Age: 18, Active: false}) 更新正常 u.WithContext(ctx).Where(u.ID.Eq(111)).Updates(model.User{Name: "hello", Age: 0, Active: false}))更新失败
select指定更新的field
很抱歉在上面问题阐述的时候使用的是官网文档上示例代码进行的阐述,在实际使用使用如下代码出现的问题:
query.AvtFile.Where(query.AvtFile.ID.Eq(file.ID)).Updates(model.AvtFile{ Name: file.Name,, Type: file.Type, File: false, file.File, UpdateTime: file.UpdateTime, IsDelete: file.IsDelete, Status: file.Status, }
其中当IsDelete为0的时候没有报错返回,数据库IsDelete字段没有更新,其他字段数据成功更新,而当IsDelete为大于0或者其他数字的时候则没有这个问题,IsDelete的类型为int64,而当使用如下代码:
query.AvtFile.Where(query.AvtFile.ID.Eq(file.ID)).Updates(map[string]interface{}{ "name": file.Name, "type": file.Type, "file": file.File, "update_time": file.UpdateTime, "is_delete": file.IsDelete, "status": file.Status, }),的时候,无论IsDelete为0还是其他数字,数据库都会更新成功
@Anniext 是符合预期的哈 https://gorm.io/docs/update.html#Updates-multiple-columns 没有办法区分你的0是默认值还是你设置的哈
@qqxhb 根据这个文档,如果想要更新所有列,需要Select("*")。
// Select all fields (select all fields include zero value fields)
db.Model(&user).Select("*").Updates(User{Name: "jinzhu", Role: "admin", Age: 0})
但是实际测试发现,Select(u.ALL) 不能更新0值的列(ALL是自动生成的,field.Asterisk)。
// 不会更新0值
_, err := u.WithContext(ctx).Where(u.ID.Eq(entity.ID)).Select(u.ALL).Updates(entity)
// 会更新0值
var err = p.platformDb.GetDb().Table((&db_model.GiftCompareTask{}).TableName()).Select("*").Updates(entity).Error
@qqxhb 根据这个文档,如果想要更新所有列,需要Select("*")。
// Select all fields (select all fields include zero value fields) db.Model(&user).Select("*").Updates(User{Name: "jinzhu", Role: "admin", Age: 0})但是实际测试发现,Select(u.ALL) 不能更新0值的列(ALL是自动生成的,field.Asterisk)。
// 不会更新0值 _, err := u.WithContext(ctx).Where(u.ID.Eq(entity.ID)).Select(u.ALL).Updates(entity) // 会更新0值 var err = p.platformDb.GetDb().Table((&db_model.GiftCompareTask{}).TableName()).Select("*").Updates(entity).Error
升级下gorm的版本吧,新的修了这个问题;或者是用field.ALL
@qqxhb 试了试,field.ALL 成功解决了这个问题
@qqxhb 根据这个文档,如果想要更新所有列,需要Select("*")。
// Select all fields (select all fields include zero value fields) db.Model(&user).Select("*").Updates(User{Name: "jinzhu", Role: "admin", Age: 0})但是实际测试发现,Select(u.ALL) 不能更新0值的列(ALL是自动生成的,field.Asterisk)。
// 不会更新0值 _, err := u.WithContext(ctx).Where(u.ID.Eq(entity.ID)).Select(u.ALL).Updates(entity) // 会更新0值 var err = p.platformDb.GetDb().Table((&db_model.GiftCompareTask{}).TableName()).Select("*").Updates(entity).Error升级下gorm的版本吧,新的修了这个问题;或者是用field.ALL
@qqxhb 我把gorm和gen都升级到最新的release版本后,这个问题依旧存在,u.WithContext(ctx).Where(u.ID.Eq(entity.ID)).Select(u.ALL).Updates(entity),生成出来的SQL,还有忽略了0值。只有field.ALL没有问题。
所以是gorm处理不了,"?.*"这种逻辑吗?