gorm 遇到的问题

说明:golang语言中对数据库的操作用的是:github.com/jinzhu/gorm,在使用的过程中遇到的一些特殊情况

一、字段设置为' '、0、fasle的情况

1、数据类型为map类型

func IndexController(c *gin.Context) error {
    params := map[string]interface{}{
        "name" : "测试map更新",
        "age" : 0,
        "phone" : "",
    dbs.GMysql["partners_manage"].Table("users").
        Where("id = 7").Debug().
        Updates(params)   //注:不要使用&
     //UPDATE `users` SET `age` = 0, `name` = '测试map更新', `phone` = ''  WHERE (id = 7) 
    c.JSON(http.StatusOK,gin.H{
        "error" : "",
        "errno" : "0",
        "dataType" : "OBJECT",
        "data" : "",
    return nil
type Users struct {
    //json_name
    Id int32 `gorm:"column:id;" json:"id"`
    Name string `gorm:"column:name;" json:"name"`
    Age int32 `gorm:"column:age;" json:"age"`
    CreatedAt string `gorm:"column:created_at;" json:"created_at"`
    Phone string `gorm:"column:phone;" json:"phone"`
    Status int32 `gorm:"column:status;" json:"status"`

2、表字段设置为sql类型

func IndexController(c *gin.Context) error {
    params := Users{
        Name: "测试strcut",
        Age: sql.NullInt32{0,true},
        Phone: sql.NullString{"",true},
    dbs.GMysql["partners_manage"].Table("users").
        Where("id = 8").Debug().
        Updates(&params)
    //UPDATE `users` SET `age` = '0', `name` = '测试strcut', `phone` = ''  WHERE (id = 8)
    c.JSON(http.StatusOK,gin.H{
        "error" : "",
        "errno" : "0",
        "dataType" : "OBJECT",
        "data" : "",
    return nil
type Users struct {
    //json_name
    Id int32 `gorm:"column:id;" json:"id"`
    Name string `gorm:"column:name;" json:"name"`
    Age sql.NullInt32 `gorm:"column:age;" json:"age"`
    CreatedAt string `gorm:"column:created_at;" json:"created_at"`
    Phone sql.NullString `gorm:"column:phone;" json:"phone"`
    Status int32 `gorm:"column:status;" json:"status"`

3、表字段设置为指针类型

func IndexController(c *gin.Context) error {
    age := 0
    phone := ""
    params := Users{
        Name: "测试指针类型",
        Age: &age,
        Phone: &phone,
    dbs.GMysql["partners_manage"].Table("users").
        Where("id = 8").Debug().
        Updates(&params)
    //UPDATE `users` SET `age` = 0, `name` = '测试指针类型', `phone` = ''  WHERE (id = 8) 
    c.JSON(http.StatusOK,gin.H{
        "error" : "",
        "errno" : "0",
        "dataType" : "OBJECT",
        "data" : "",
    return nil
type Users struct {
    //json_name
    Id int32 `gorm:"column:id;" json:"id"`
    Name string `gorm:"column:name;" json:"name"`
    Age *int `gorm:"column:age;" json:"age"`
    CreatedAt string `gorm:"column:created_at;" json:"created_at"`
    Phone *string `gorm:"column:phone;" json:"phone"`
    Status int32 `gorm:"column:status;" json:"status"`