MustCols: update cols bug !!! #868

Closed
opened 2018-03-24 05:47:28 +00:00 by fancyecommerce · 4 comments
fancyecommerce commented 2018-03-24 05:47:28 +00:00 (Migrated from github.com)


type CustomerUpdate struct {
    Id int64 `form:"id" json:"id"`
    Username string `form:"username" json:"username" binding:"required"`
    Email string `form:"email" json:"email"`
    Sex int `form:"sex" json:"sex"`
    Name string `form:"name" json:"name" binding:"required"`
    Telephone string `form:"telephone" json:"telephone"`
    Type int `form:"type" json:"type" binding:"required"`
    ParentId int64 `form:"parent_id" json:"parent_id" xorm:"int null"`
    Remark string `form:"remark" json:"remark"`
    Status int `form:"status" json:"status" binding:"required"`
    Age int `form:"age" json:"age"`
    CreatedAt int64 `xorm:"created" form:"created_at" json:"created_at"`
    UpdatedAt int64 `xorm:"updated" form:"updated_at" json:"updated_at"`
    BirthDate  int64 `form:"birth_date" json:"birth_date"`
    Password string `xorm:"varchar(200)" form:"password" json:"password"`
}


/**
 * 通过id为条件,更新一条记录
 */
func CustomerUpdateById(c *gin.Context){
    var customer CustomerUpdate
    err := c.ShouldBindJSON(&customer);
    if err != nil {
        c.AbortWithStatusJSON(http.StatusOK, util.BuildFailResult(err.Error()))
        return
    }
    passwordEncry, err := getCustomerPassword(customer.Password)
    if err != nil {
        c.AbortWithStatusJSON(http.StatusOK, util.BuildFailResult(err.Error()))
        return
    } 
    customer.Password = passwordEncry
    
    // 处理账户类型
    if customer.Type == AdminChildType {
        if customer.ParentId == 0 {
            c.AbortWithStatusJSON(http.StatusOK, util.BuildFailResult("child admin account , Must fill in parent account "))
            return
        }
        parentCustomer, err := GetCustomerOneById(customer.ParentId)
        if err != nil {
            c.AbortWithStatusJSON(http.StatusOK, util.BuildFailResult(err.Error()))
            return
        } 
        if parentCustomer.Type != AdminCommonType {
            c.AbortWithStatusJSON(http.StatusOK, util.BuildFailResult("parent account is incorrect"))
            return
        }
    } else {
        customer.ParentId = 0
    }
    
    affected, err := engine.MustCols("parent_id").Update(&customer, &Customer{Id:customer.Id})
    if err != nil {
        c.AbortWithStatusJSON(http.StatusOK, util.BuildFailResult(err.Error()))
        return
    } 
    result := util.BuildSuccessResult(gin.H{
        "affected":affected,
        "customer":customer,
    })
    c.JSON(http.StatusOK, result)
}

update fail: affected, err := engine.MustCols("parent_id").Update(&customer, &Customer{Id:customer.Id})

``` type CustomerUpdate struct { Id int64 `form:"id" json:"id"` Username string `form:"username" json:"username" binding:"required"` Email string `form:"email" json:"email"` Sex int `form:"sex" json:"sex"` Name string `form:"name" json:"name" binding:"required"` Telephone string `form:"telephone" json:"telephone"` Type int `form:"type" json:"type" binding:"required"` ParentId int64 `form:"parent_id" json:"parent_id" xorm:"int null"` Remark string `form:"remark" json:"remark"` Status int `form:"status" json:"status" binding:"required"` Age int `form:"age" json:"age"` CreatedAt int64 `xorm:"created" form:"created_at" json:"created_at"` UpdatedAt int64 `xorm:"updated" form:"updated_at" json:"updated_at"` BirthDate int64 `form:"birth_date" json:"birth_date"` Password string `xorm:"varchar(200)" form:"password" json:"password"` } /** * 通过id为条件,更新一条记录 */ func CustomerUpdateById(c *gin.Context){ var customer CustomerUpdate err := c.ShouldBindJSON(&customer); if err != nil { c.AbortWithStatusJSON(http.StatusOK, util.BuildFailResult(err.Error())) return } passwordEncry, err := getCustomerPassword(customer.Password) if err != nil { c.AbortWithStatusJSON(http.StatusOK, util.BuildFailResult(err.Error())) return } customer.Password = passwordEncry // 处理账户类型 if customer.Type == AdminChildType { if customer.ParentId == 0 { c.AbortWithStatusJSON(http.StatusOK, util.BuildFailResult("child admin account , Must fill in parent account ")) return } parentCustomer, err := GetCustomerOneById(customer.ParentId) if err != nil { c.AbortWithStatusJSON(http.StatusOK, util.BuildFailResult(err.Error())) return } if parentCustomer.Type != AdminCommonType { c.AbortWithStatusJSON(http.StatusOK, util.BuildFailResult("parent account is incorrect")) return } } else { customer.ParentId = 0 } affected, err := engine.MustCols("parent_id").Update(&customer, &Customer{Id:customer.Id}) if err != nil { c.AbortWithStatusJSON(http.StatusOK, util.BuildFailResult(err.Error())) return } result := util.BuildSuccessResult(gin.H{ "affected":affected, "customer":customer, }) c.JSON(http.StatusOK, result) } ``` update fail: ` affected, err := engine.MustCols("parent_id").Update(&customer, &Customer{Id:customer.Id})`
fancyecommerce commented 2018-03-24 05:49:03 +00:00 (Migrated from github.com)

use the above code to set parnet_id = 0

return var affected is 0

use the above code to set parnet_id = 0 return var affected is 0
fancyecommerce commented 2018-03-24 06:39:03 +00:00 (Migrated from github.com)

after i change code

affected, err := engine.MustCols("parent_id").Update(&customer, &Customer{Id:customer.Id})

to

affected, err := engine.In("id", customer.Id).AllCols().Update(&customer)

update effect!so, it is a bug!

after i change code ``` affected, err := engine.MustCols("parent_id").Update(&customer, &Customer{Id:customer.Id}) ``` to ``` affected, err := engine.In("id", customer.Id).AllCols().Update(&customer) ``` update effect!so, it is a bug!
fancyecommerce commented 2018-03-24 06:43:58 +00:00 (Migrated from github.com)

the following code is effect

 affected, err := engine.In("id", customer.Id).MustCols("parent_id").Update(&customer)
affected, err := engine.Where("id = ?", customer.Id).MustCols("parent_id").Update(&customer)

so i think , if sql condition write to update function,like .Update(&customer, &Customer{Id:customer.Id}), update will fail

the following code is effect ``` affected, err := engine.In("id", customer.Id).MustCols("parent_id").Update(&customer) affected, err := engine.Where("id = ?", customer.Id).MustCols("parent_id").Update(&customer) ``` so i think , if sql condition write to update function,like `.Update(&customer, &Customer{Id:customer.Id})`, update will fail

Please confirm #886 closed this one.

Please confirm #886 closed this one.
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#868
No description provided.