瀏覽代碼

Fix BUG: Update statement build function should ingore ONLYFROMDB fields

* Fix BUG: Update statement build function should ingore ONLYFROMDB fields

* Add test case

* Modify test case
xormplus 7 年之前
父節點
當前提交
0f556a8257
共有 2 個文件被更改,包括 39 次插入0 次删除
  1. 4 0
      statement.go
  2. 35 0
      statement_test.go

+ 4 - 0
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)

+ 35 - 0
statement_test.go

@@ -9,6 +9,7 @@ import (
 	"strings"
 	"testing"
 
+	"github.com/stretchr/testify/assert"
 	"github.com/xormplus/core"
 )
 
@@ -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()
+}