From f16ce722ec150ade2c2e5a08a6fed64706c79465 Mon Sep 17 00:00:00 2001 From: "DrWrong(Yuhang)" Date: Sat, 23 Jun 2018 14:59:01 +0800 Subject: [PATCH] Fix BUG: Update statement build function should ingore ONLYFROMDB fields (#1012) * Fix BUG: Update statement build function should ingore ONLYFROMDB fields * Add test case * Modify test case --- statement.go | 4 ++++ statement_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/statement.go b/statement.go index c67d8c01c..54c6006b7 100644 --- a/statement.go +++ b/statement.go @@ -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) diff --git a/statement_test.go b/statement_test.go index 8a7667f5f..acca72dab 100644 --- a/statement_test.go +++ b/statement_test.go @@ -203,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() +}