From be45d8312eec9b2280b9564836a60d31bf7d3536 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 4 Jan 2016 19:22:42 +0800 Subject: [PATCH] Create index automatically when create table --- scope_private.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scope_private.go b/scope_private.go index b0d7be52..761241af 100644 --- a/scope_private.go +++ b/scope_private.go @@ -557,7 +557,10 @@ func (scope *Scope) createTable() *Scope { if len(primaryKeys) > 0 && !primaryKeyInColumnType { primaryKeyStr = fmt.Sprintf(", PRIMARY KEY (%v)", strings.Join(primaryKeys, ",")) } + scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v) %s", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr, scope.getTableOptions())).Exec() + + scope.autoIndex() return scope } @@ -629,9 +632,8 @@ func (scope *Scope) autoMigrate() *Scope { } scope.createJoinTable(field) } + scope.autoIndex() } - - scope.autoIndex() return scope }