From 36043ad905ae3c19feaebd68327b1bf6b291ec29 Mon Sep 17 00:00:00 2001 From: matematik7 Date: Mon, 4 Sep 2017 18:12:20 +0200 Subject: [PATCH] Fix for quoted column names and add test --- query_test.go | 24 ++++++++++++++++++++++++ scope.go | 8 ++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/query_test.go b/query_test.go index 882fd611..df8893fd 100644 --- a/query_test.go +++ b/query_test.go @@ -674,3 +674,27 @@ func TestSelectWithArrayInput(t *testing.T) { t.Errorf("Should have selected both age and name") } } + +func TestPluckWithSelect(t *testing.T) { + DB.Save(&User{Name: "matematik7", Age: 25}) + + var userAges []string + err := DB.Model(&User{}).Where("age = ?", 25).Select("name || ' - ' || age as user_age").Pluck("user_age", &userAges).Error + if err != nil { + t.Error(err) + } + + if len(userAges) != 1 || userAges[0] != "matematik7 - 25" { + t.Errorf("Should correctly pluck with select, got: %s", userAges) + } + + userAges = userAges[:0] + err = DB.Model(&User{}).Where("age = ?", 25).Select("name || ' - ' || age as \"user_age\"").Pluck("user_age", &userAges).Error + if err != nil { + t.Error(err) + } + + if len(userAges) != 1 || userAges[0] != "matematik7 - 25" { + t.Errorf("Should correctly pluck with select, got: %s", userAges) + } +} diff --git a/scope.go b/scope.go index db797dcc..65ac62d9 100644 --- a/scope.go +++ b/scope.go @@ -939,12 +939,16 @@ func (scope *Scope) initialize() *Scope { } func (scope *Scope) isQueryForColumn(query interface{}, column string) bool { - queryStr := fmt.Sprint(query) + queryStr := strings.ToLower(fmt.Sprint(query)) if queryStr == column { return true } - if strings.HasSuffix(strings.ToLower(queryStr), "as "+column) { + if strings.HasSuffix(queryStr, "as "+column) { + return true + } + + if strings.HasSuffix(queryStr, "as \""+column+"\"") { return true }