diff --git a/circle.yml b/circle.yml index 27f7f98a..c281a6a2 100644 --- a/circle.yml +++ b/circle.yml @@ -22,8 +22,8 @@ test: override: # './...' is a relative pattern which means all subdirectories - go test -v -race - - go test -db=mysql -conn_str="root:@/xorm_test" - - go test -db=postgres -conn_str="dbname=xorm_test sslmode=disable" + - go test -v -race -db=mysql -conn_str="root:@/xorm_test" + - go test -v -race -db=postgres -conn_str="dbname=xorm_test sslmode=disable" - cd /home/ubuntu/.go_workspace/src/github.com/go-xorm/tests && ./sqlite3.sh - cd /home/ubuntu/.go_workspace/src/github.com/go-xorm/tests && ./mysql.sh - cd /home/ubuntu/.go_workspace/src/github.com/go-xorm/tests && ./postgres.sh \ No newline at end of file diff --git a/engine.go b/engine.go index a788c117..5e8d0af9 100644 --- a/engine.go +++ b/engine.go @@ -1007,6 +1007,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 @@ -1014,9 +1018,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 { diff --git a/engine_test.go b/engine_test.go new file mode 100644 index 00000000..fcd3953c --- /dev/null +++ b/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) +} diff --git a/xorm.go b/xorm.go index c22c1b65..63cb15a6 100644 --- a/xorm.go +++ b/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 {