为了账号安全,请及时绑定邮箱和手机立即绑定

GORM 不会将布尔字段更新为 false

GORM 不会将布尔字段更新为 false

Go
富国沪深 2023-06-26 16:39:35
在updatesgorm 上,不会将布尔类型更新为false. 默认情况下它更新为true,但是当我尝试更新为false不更改时。我也没有看到任何错误。可能是什么问题?type Attendee struct {    ID             uint   `gorm:"primary_key" gorm:"AUTO_INCREMENT" json:"id,omitempty" mapstructure:"id" csv:"ID"`    Email          string `json:"email,omitempty" mapstructure:"email" csv:"Email,required"`    ShowDirectory  bool   `json:"show_directory,omitempty" gorm:"default:true" mapstructure:"show_directory" csv:"-"`}var attendee Attendee// JSON.unmarshal lines here for the &attendeeif err := service.DB.Model(&attendee).Updates(Attendee{        Email:         attendee.Email,        ShowDirectory: false}).Error; err != nil {    return Attendee{}, err}替代解决方案:这有效,但我正在更新多个属性。所以,我不能用这个。    att := Attendee{ID: 1}    service.DB.Model(&att).Update("ShowDirectory", false)
查看完整描述

5 回答

?
饮歌长啸

TA贡献1951条经验 获得超3个赞

// Update attributes with `struct`, will only update non-zero fields

db.Model(&user).Updates(User{Name: "hello", Age: 18, Active: false})

// UPDATE users SET name='hello', age=18, updated_at = '2013-11-17 > 21:34:10' WHERE id = 111;


// Update attributes with `map`

db.Model(&user).Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})

// UPDATE users SET name='hello', age=18, actived=false, updated_at='2013-11-17 21:34:10' WHERE id=111;

注意当使用struct更新时,GORM只会更新非零字段,您可能想使用map更新属性或使用Select指定要更新的字段


解决了:


if err := service.DB.Model(&attendee).Updates(map[string]interface{}{

    "Email":          attendee.Email,

    "ShowDirectory": false

}).Error; err != nil {

    return Attendee{}, err

}


查看完整回答
反对 回复 2023-06-26
?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

另一种方便的方法是将该字段设置为指针。

注意所有具有零值的字段,如 0、''、false 或其他零值,都不会保存到数据库中,但会使用其默认值。如果您想避免这种情况,请考虑使用指针类型或扫描仪/估价器

在您的情况下,模型将如下所示:

type Attendee struct {

        ID             uint   `gorm:"primary_key" gorm:"AUTO_INCREMENT" json:"id,omitempty" mapstructure:"id" csv:"ID"`

        Email          string `json:"email,omitempty" mapstructure:"email" csv:"Email,required"`

    

        ShowDirectory  *bool   `json:"show_directory,omitempty" gorm:"default:true" mapstructure:"show_directory" csv:"-"`

}


查看完整回答
反对 回复 2023-06-26
?
德玛西亚99

TA贡献1770条经验 获得超3个赞

正如文档中提到的,要设置虚假值,您需要使用map或选择所需的列。


当使用struct更新时,GORM只会更新非零字段,您可能需要使用map来更新属性或使用Select来指定要更新的字段


// Select with Struct (select zero value fields)

db.Model(&user).Select("Name", "Age").Updates(User{Name: "new_name", Age: 0})

// UPDATE users SET name='new_name', age=0 WHERE id=111;

或者您可以选择所有列:


// Select all fields (select all fields include zero value fields)

db.Model(&user).Select("*").Update(User{Name: "jinzhu", Role: "admin", Age: 0})


查看完整回答
反对 回复 2023-06-26
?
撒科打诨

TA贡献1934条经验 获得超2个赞

请不要使用 go struct 来更新非零字段,例如 boolean:false


下面的代码不会Active: false在你的数据库中更新,gorm 会忽略


db.Model(&user).Updates(User{Name: "hello", Age: 18, Active: false})

// UPDATE users SET name='hello', age=18, updated_at = '2013-11-17 21:34:10' WHERE id = 111;

下面的代码将更新Active: false


db.Model(&user).Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})

// UPDATE users SET name='hello', age=18, actived=false, updated_at='2013-11-17 21:34:10' WHERE id=111;

使用 Map 而不是 go struct


查看完整回答
反对 回复 2023-06-26
?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

您应该在结构中编写 gorm 类型,如下所示: gorm:"type:boolean; column:column_name" 并且它肯定会起作用!



查看完整回答
反对 回复 2023-06-26
  • 5 回答
  • 0 关注
  • 319 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信