From 88188b6161192b0c5b8fa233de03e83b4d14d475 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 1 Oct 2015 06:53:15 +0800 Subject: [PATCH] Respect Where when create index --- preload_test.go | 2 +- scope_private.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/preload_test.go b/preload_test.go index 490c3134..de00f529 100644 --- a/preload_test.go +++ b/preload_test.go @@ -816,7 +816,7 @@ func TestNilPointerSlice(t *testing.T) { } var got []Level1 - if err := DB.Debug().Preload("Level2").Preload("Level2.Level3").Find(&got).Error; err != nil { + if err := DB.Preload("Level2").Preload("Level2.Level3").Find(&got).Error; err != nil { panic(err) } diff --git a/scope_private.go b/scope_private.go index c16bd129..c743c119 100644 --- a/scope_private.go +++ b/scope_private.go @@ -579,7 +579,9 @@ func (scope *Scope) addIndex(unique bool, indexName string, column ...string) { sqlCreate = "CREATE UNIQUE INDEX" } - scope.Raw(fmt.Sprintf("%s %v ON %v(%v);", sqlCreate, indexName, scope.QuotedTableName(), strings.Join(columns, ", "))).Exec() + scope.Search.Unscoped = true + scope.Raw(fmt.Sprintf("%s %v ON %v(%v) %v", sqlCreate, indexName, scope.QuotedTableName(), strings.Join(columns, ", "), scope.whereSql())).Exec() + scope.Search.Unscoped = false } func (scope *Scope) addForeignKey(field string, dest string, onDelete string, onUpdate string) {