fix bug for int64 id tag

This commit is contained in:
Lunny Xiao 2017-05-17 17:05:36 +08:00
parent 4bd47ece79
commit 1ff7790a2e
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
4 changed files with 66 additions and 6 deletions

View File

@ -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

View File

@ -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 {

59
engine_test.go Normal file
View File

@ -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)
}

View File

@ -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 {