fix bug on update (#676)

This commit is contained in:
Lunny Xiao 2017-08-05 10:46:29 +08:00 committed by GitHub
parent 10faec1e58
commit 0fdfa8d8de
2 changed files with 33 additions and 0 deletions

View File

@ -1123,3 +1123,33 @@ func TestNoUpdate(t *testing.T) {
assert.Error(t, err)
assert.EqualValues(t, "No content found to be updated", err.Error())
}
func TestNewUpdate(t *testing.T) {
assert.NoError(t, prepareEngine())
type TbUserInfo struct {
Id int64 `xorm:"pk autoincr unique BIGINT" json:"id"`
Phone string `xorm:"not null unique VARCHAR(20)" json:"phone"`
UserName string `xorm:"VARCHAR(20)" json:"user_name"`
Gender int `xorm:"default 0 INTEGER" json:"gender"`
Pw string `xorm:"VARCHAR(100)" json:"pw"`
Token string `xorm:"TEXT" json:"token"`
Avatar string `xorm:"TEXT" json:"avatar"`
Extras interface{} `xorm:"JSON" json:"extras"`
Created time.Time `xorm:"DATETIME created"`
Updated time.Time `xorm:"DATETIME updated"`
Deleted time.Time `xorm:"DATETIME deleted"`
}
assertSync(t, new(TbUserInfo))
targetUsr := TbUserInfo{Phone: "13126564922"}
changeUsr := TbUserInfo{Token: "ABCDEFG"}
af, err := testEngine.Update(&changeUsr, &targetUsr)
assert.NoError(t, err)
assert.EqualValues(t, 0, af)
af, err = testEngine.Table(new(TbUserInfo)).Where("phone=?", 13126564922).Update(&changeUsr)
assert.NoError(t, err)
assert.EqualValues(t, 0, af)
}

View File

@ -272,6 +272,9 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
fieldValue := *fieldValuePtr
fieldType := reflect.TypeOf(fieldValue.Interface())
if fieldType == nil {
continue
}
requiredField := useAllCols
includeNil := useAllCols