소스 검색

fix bug for int64 id tag

xormplus 8 년 전
부모
커밋
1aeba421dc
3개의 변경된 파일64개의 추가작업 그리고 4개의 파일을 삭제
  1. 4 3
      engine.go
  2. 59 0
      engine_test.go
  3. 1 1
      xorm.go

+ 4 - 3
engine.go

@@ -1016,6 +1016,10 @@ func (engine *Engine) mapType(v reflect.Value) (*core.Table, error) {
 			col = core.NewColumn(engine.ColumnMapper.Obj2Table(t.Field(i).Name),
 				t.Field(i).Name, sqlType, sqlType.DefaultLength,
 				sqlType.DefaultLength2, true)
+
+			if fieldType.Kind() == reflect.Int64 && (strings.ToUpper(col.FieldName) == "ID" || strings.HasSuffix(strings.ToUpper(col.FieldName), ".ID")) {
+				idFieldColName = col.Name
+			}
 		}
 		if col.IsAutoIncrement {
 			col.Nullable = false
@@ -1023,9 +1027,6 @@ func (engine *Engine) mapType(v reflect.Value) (*core.Table, error) {
 
 		table.AddColumn(col)
 
-		if fieldType.Kind() == reflect.Int64 && (strings.ToUpper(col.FieldName) == "ID" || strings.HasSuffix(strings.ToUpper(col.FieldName), ".ID")) {
-			idFieldColName = col.Name
-		}
 	} // end for
 
 	if idFieldColName != "" && len(table.PrimaryKeys) == 0 {

+ 59 - 0
engine_test.go

@@ -0,0 +1,59 @@
+// Copyright 2017 The Xorm Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package xorm
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestAutoIncrTag(t *testing.T) {
+	assert.NoError(t, prepareEngine())
+
+	type TestAutoIncr1 struct {
+		Id int64
+	}
+
+	tb := testEngine.TableInfo(new(TestAutoIncr1))
+	cols := tb.Columns()
+	assert.EqualValues(t, 1, len(cols))
+	assert.True(t, cols[0].IsAutoIncrement)
+	assert.True(t, cols[0].IsPrimaryKey)
+	assert.Equal(t, "id", cols[0].Name)
+
+	type TestAutoIncr2 struct {
+		Id int64 `xorm:"id"`
+	}
+
+	tb = testEngine.TableInfo(new(TestAutoIncr2))
+	cols = tb.Columns()
+	assert.EqualValues(t, 1, len(cols))
+	assert.False(t, cols[0].IsAutoIncrement)
+	assert.False(t, cols[0].IsPrimaryKey)
+	assert.Equal(t, "id", cols[0].Name)
+
+	type TestAutoIncr3 struct {
+		Id int64 `xorm:"'ID'"`
+	}
+
+	tb = testEngine.TableInfo(new(TestAutoIncr3))
+	cols = tb.Columns()
+	assert.EqualValues(t, 1, len(cols))
+	assert.False(t, cols[0].IsAutoIncrement)
+	assert.False(t, cols[0].IsPrimaryKey)
+	assert.Equal(t, "ID", cols[0].Name)
+
+	type TestAutoIncr4 struct {
+		Id int64 `xorm:"pk"`
+	}
+
+	tb = testEngine.TableInfo(new(TestAutoIncr4))
+	cols = tb.Columns()
+	assert.EqualValues(t, 1, len(cols))
+	assert.False(t, cols[0].IsAutoIncrement)
+	assert.True(t, cols[0].IsPrimaryKey)
+	assert.Equal(t, "id", cols[0].Name)
+}

+ 1 - 1
xorm.go

@@ -17,7 +17,7 @@ import (
 
 const (
 	// Version show the xorm's version
-	Version string = "0.6.2.0412"
+	Version string = "0.6.2.0517"
 )
 
 func regDrvsNDialects() bool {