更新方法不更新零值

原始问题


在 GORM 中使用该Update方法时,不会保存新数据。即我想设置一个boolfrom trueto false,但即使在Update方法之后它仍然保持真实。


在该方法的描述中有一条警告:“使用结构更新时警告,GORM 不会更新零值的字段”


由于我使用结构来更新并且false是 的零值bool,这似乎是预期的行为,但我看不出有任何理由这样做以及如何克服这一点。


func UpdateData(c *fiber.Ctx) error {

    db := database.DBConn


    data := new([]entities.Data)


    if err := c.BodyParser(&data); err != nil {

        return err

    }


    db.Update(&data)

    return c.JSON(data)

}

解决方案总结


new首先,正如建议的那样,我在实例化结构时省略了关键字。然后,我使用了一个辅助函数(从这里开始)将结构转换为映射,同时将 json 别名作为键:


// StructToMap Converts a struct to a map while maintaining the json alias as keys

func StructToMap(obj interface{}) (newMap map[string]interface{}, err error) {

    data, err := json.Marshal(obj)


    if err != nil {

        return

    }


    err = json.Unmarshal(data, &newMap) // Convert to a map

    return

}

然后我循环遍历数据切片中的每个元素,以便对其进行转换并一一更新:


func UpdateData(c *fiber.Ctx) error {

    db := database.DBConn


    data := []entities.Dard{}


    if err := c.BodyParser(&data); err != nil {

        return err

    }


    for _, record := range data {

        mappedData, _ := StructToMap(record)

        db.Model(&entities.Data{}).Update(mappedData)

    }


    return c.JSON(data)

}

*在这个例子中,错误处理明显减少了。


至尊宝的传说
浏览 230回答 1
1回答

眼眸繁星

来自官方文档注意使用 struct 更新时,GORM 只会更新非零字段,您可能希望使用 map 更新属性或使用 Select 指定要更新的字段因此map[string]interface{}也用于更新非零字段。例子:db.Model(&user).Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})由于您已经拥有 struct ,因此您可以转换struct为map[string]interface{}(请参阅有关转换的详细信息)然后更新。另一种方法是将字段类型更改为指针。
打开App,查看更多内容
随时随地看视频慕课网APP