From 62b447bc9ac8b655f0f02b3dbab90a4a84289a43 Mon Sep 17 00:00:00 2001 From: ryanss Date: Sat, 15 Nov 2014 23:31:00 -0500 Subject: [PATCH] Create tables with fields in same order as defined in struct --- scope_private.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/scope_private.go b/scope_private.go index debea2b7..b9004efd 100644 --- a/scope_private.go +++ b/scope_private.go @@ -512,12 +512,17 @@ func (scope *Scope) createJoinTable(field *Field) { func (scope *Scope) createTable() *Scope { var sqls []string - for _, field := range scope.Fields() { - if field.IsNormal { - sqlTag := scope.sqlTagForField(field) - sqls = append(sqls, scope.Quote(field.DBName)+" "+sqlTag) + fields := scope.Fields() + scopeType := scope.IndirectValue().Type() + for i := 0; i < scopeType.NumField(); i++ { + for _, field := range scope.fieldFromStruct(scopeType.Field(i), false) { + field = fields[field.DBName] + if field.IsNormal { + sqlTag := scope.sqlTagForField(field) + sqls = append(sqls, scope.Quote(field.DBName)+" "+sqlTag) + } + scope.createJoinTable(field) } - scope.createJoinTable(field) } scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v)", scope.QuotedTableName(), strings.Join(sqls, ","))).Exec() return scope