From 841ea1bde530b7d046262861cc39a041f42bdce3 Mon Sep 17 00:00:00 2001 From: matematik7 Date: Mon, 14 Aug 2017 20:46:39 +0200 Subject: [PATCH] Do not always override select on pluck --- scope.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/scope.go b/scope.go index 0dcea855..db797dcc 100644 --- a/scope.go +++ b/scope.go @@ -938,14 +938,30 @@ func (scope *Scope) initialize() *Scope { return scope } +func (scope *Scope) isQueryForColumn(query interface{}, column string) bool { + queryStr := fmt.Sprint(query) + if queryStr == column { + return true + } + + if strings.HasSuffix(strings.ToLower(queryStr), "as "+column) { + return true + } + + return false +} + func (scope *Scope) pluck(column string, value interface{}) *Scope { dest := reflect.Indirect(reflect.ValueOf(value)) - scope.Search.Select(column) if dest.Kind() != reflect.Slice { scope.Err(fmt.Errorf("results should be a slice, not %s", dest.Kind())) return scope } + if query, ok := scope.Search.selects["query"]; !ok || !scope.isQueryForColumn(query, column) { + scope.Search.Select(column) + } + rows, err := scope.rows() if scope.Err(err) == nil { defer rows.Close()