Skip to content

Commit

Permalink
Fix BUG: Update statement build function should ingore ONLYFROMDB fields
Browse files Browse the repository at this point in the history
* Fix BUG: Update statement build function should ingore ONLYFROMDB fields

* Add test case

* Modify test case
  • Loading branch information
xormplus committed Jun 23, 2018
1 parent 6d64dab commit 0f556a8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ func (statement *Statement) buildUpdates(bean interface{},
continue
}

if col.MapType == core.ONLYFROMDB {
continue
}

fieldValuePtr, err := col.ValueOf(bean)
if err != nil {
engine.logger.Error(err)
Expand Down
35 changes: 35 additions & 0 deletions statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/xormplus/core"
)

Expand Down Expand Up @@ -202,3 +203,37 @@ func TestDistinctAndCols(t *testing.T) {
assert.EqualValues(t, 1, len(names))
assert.EqualValues(t, "test", names[0])
}

func TestUpdateIgnoreOnlyFromDBFields(t *testing.T) {
type TestOnlyFromDBField struct {
Id int64 `xorm:"PK"`
OnlyFromDBField string `xorm:"<-"`
OnlyToDBField string `xorm:"->"`
IngoreField string `xorm:"-"`
}

assertGetRecord := func() *TestOnlyFromDBField {
var record TestOnlyFromDBField
has, err := testEngine.Where("id = ?", 1).Get(&record)
assert.NoError(t, err)
assert.EqualValues(t, true, has)
assert.EqualValues(t, "", record.OnlyFromDBField)
return &record

}
assert.NoError(t, prepareEngine())
assertSync(t, new(TestOnlyFromDBField))

_, err := testEngine.Insert(&TestOnlyFromDBField{
Id: 1,
OnlyFromDBField: "a",
OnlyToDBField: "b",
IngoreField: "c",
})
assert.NoError(t, err)

record := assertGetRecord()
record.OnlyFromDBField = "test"
testEngine.Update(record)
assertGetRecord()
}

0 comments on commit 0f556a8

Please sign in to comment.