Fix for quoted column names and add test

This commit is contained in:
matematik7 2017-09-04 18:12:20 +02:00 committed by Jinzhu
parent 841ea1bde5
commit 36043ad905
2 changed files with 30 additions and 2 deletions

View File

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

View File

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