mirror of https://github.com/go-gorm/gorm.git
Fix for quoted column names and add test
This commit is contained in:
parent
841ea1bde5
commit
36043ad905
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
8
scope.go
8
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue