Uniq foreign key for Preload

This commit is contained in:
Jinzhu 2015-02-24 15:50:02 +08:00
parent a97a508ec7
commit 5586d04999
1 changed files with 5 additions and 1 deletions

View File

@ -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()}