From 5586d04999317056c98f0ece11f991563100ab4b Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 24 Feb 2015 15:50:02 +0800 Subject: [PATCH] Uniq foreign key for Preload --- preload.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/preload.go b/preload.go index eea75259..02e9eb38 100644 --- a/preload.go +++ b/preload.go @@ -118,8 +118,12 @@ func (scope *Scope) getColumnAsArray(column string) (primaryKeys []interface{}) values := scope.IndirectValue() switch values.Kind() { case reflect.Slice: + primaryKeyMap := map[interface{}]bool{} for i := 0; i < values.Len(); i++ { - primaryKeys = append(primaryKeys, reflect.Indirect(values.Index(i)).FieldByName(column).Interface()) + primaryKeyMap[reflect.Indirect(values.Index(i)).FieldByName(column).Interface()] = true + } + for key := range primaryKeyMap { + primaryKeys = append(primaryKeys, key) } case reflect.Struct: return []interface{}{values.FieldByName(column).Interface()}