Fix create table with struct missing columns

This commit is contained in:
Lunny Xiao 2021-06-09 11:20:56 +08:00
parent 5b624ed051
commit 099415748c
2 changed files with 77 additions and 3 deletions

View File

@ -39,6 +39,82 @@ func TestCreateTable(t *testing.T) {
assert.NoError(t, testEngine.Table("user_user").CreateTable(&UserinfoCreateTable{})) assert.NoError(t, testEngine.Table("user_user").CreateTable(&UserinfoCreateTable{}))
} }
func TestCreateTable2(t *testing.T) {
type BaseModelLogicalDel struct {
Id string `xorm:"varchar(46) pk"`
CreatedAt time.Time `xorm:"created"`
UpdatedAt time.Time `xorm:"updated"`
DeletedAt *time.Time `xorm:"deleted"`
}
type TestPerson struct {
BaseModelLogicalDel `xorm:"extends"`
UserId string `xorm:"varchar(46) notnull"`
PersonId string `xorm:"varchar(46) notnull"`
Star bool
SortNo int
DispName string `xorm:"varchar(100)"`
FirstName string
LastName string
FirstNameKana string
LastNameKana string
BirthYear *int
BirthMonth *int
BirthDay *int
ImageId string `xorm:"varchar(46)"`
ImageDefaultId string `xorm:"varchar(46)"`
UserText string `xorm:"varchar(2000)"`
GenderId *int
At1 string `xorm:"varchar(10)"`
At1Rate int
At2 string `xorm:"varchar(10)"`
At2Rate int
At3 string `xorm:"varchar(10)"`
At3Rate int
At4 string `xorm:"varchar(10)"`
At4Rate int
At5 string `xorm:"varchar(10)"`
At5Rate int
At6 string `xorm:"varchar(10)"`
At6Rate int
}
assert.NoError(t, PrepareEngine())
tb1, err := testEngine.TableInfo(TestPerson{})
assert.NoError(t, err)
tb2, err := testEngine.TableInfo(new(TestPerson))
assert.NoError(t, err)
cols1, cols2 := tb1.ColumnsSeq(), tb2.ColumnsSeq()
assert.EqualValues(t, len(cols1), len(cols2))
for i, col := range cols1 {
assert.EqualValues(t, col, cols2[i])
}
result, err := testEngine.IsTableExist(new(TestPerson))
assert.NoError(t, err)
if result {
assert.NoError(t, testEngine.DropTables(new(TestPerson)))
}
assert.NoError(t, testEngine.CreateTables(new(TestPerson)))
tables1, err := testEngine.DBMetas()
assert.NoError(t, err)
assert.Len(t, tables1, 1)
assert.EqualValues(t, len(cols1), len(tables1[0].Columns()))
result, err = testEngine.IsTableExist(new(TestPerson))
assert.NoError(t, err)
if result {
assert.NoError(t, testEngine.DropTables(new(TestPerson)))
}
assert.NoError(t, testEngine.CreateTables(TestPerson{}))
tables2, err := testEngine.DBMetas()
assert.NoError(t, err)
assert.Len(t, tables2, 1)
assert.EqualValues(t, len(cols1), len(tables2[0].Columns()))
}
func TestCreateMultiTables(t *testing.T) { func TestCreateMultiTables(t *testing.T) {
assert.NoError(t, PrepareEngine()) assert.NoError(t, PrepareEngine())

View File

@ -267,7 +267,7 @@ func (parser *Parser) Parse(v reflect.Value) (*schemas.Table, error) {
addIndex(indexName, table, col, indexType) addIndex(indexName, table, col, indexType)
} }
} }
} else if fieldValue.CanSet() { } else {
var sqlType schemas.SQLType var sqlType schemas.SQLType
if fieldValue.CanAddr() { if fieldValue.CanAddr() {
if _, ok := fieldValue.Addr().Interface().(convert.Conversion); ok { if _, ok := fieldValue.Addr().Interface().(convert.Conversion); ok {
@ -286,8 +286,6 @@ func (parser *Parser) Parse(v reflect.Value) (*schemas.Table, error) {
if fieldType.Kind() == reflect.Int64 && (strings.ToUpper(col.FieldName) == "ID" || strings.HasSuffix(strings.ToUpper(col.FieldName), ".ID")) { if fieldType.Kind() == reflect.Int64 && (strings.ToUpper(col.FieldName) == "ID" || strings.HasSuffix(strings.ToUpper(col.FieldName), ".ID")) {
idFieldColName = col.Name idFieldColName = col.Name
} }
} else {
continue
} }
if col.IsAutoIncrement { if col.IsAutoIncrement {
col.Nullable = false col.Nullable = false