Merge pull request #2411 from France-ioi/pluck

Remove old elements from the output parameter of Pluck()
This commit is contained in:
Emir Beganović 2019-05-05 09:58:14 +04:00 committed by GitHub
commit 018491a6dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -1110,6 +1110,37 @@ func TestCountWithHaving(t *testing.T) {
} }
} }
func TestPluck(t *testing.T) {
db := DB.New()
db.Delete(User{})
defer db.Delete(User{})
DB.Create(&User{Id: 1, Name: "user1"})
DB.Create(&User{Id: 2, Name: "user2"})
DB.Create(&User{Id: 3, Name: "user3"})
var ids []int64
err := db.Model(User{}).Order("id").Pluck("id", &ids).Error
if err != nil {
t.Error("Unexpected error on pluck")
}
if len(ids) != 3 || ids[0] != 1 || ids[1] != 2 || ids[2] != 3 {
t.Error("Unexpected result on pluck")
}
err = db.Model(User{}).Order("id").Pluck("id", &ids).Error
if err != nil {
t.Error("Unexpected error on pluck again")
}
if len(ids) != 3 || ids[0] != 1 || ids[1] != 2 || ids[2] != 3 {
t.Error("Unexpected result on pluck again")
}
}
func TestCountWithQueryOption(t *testing.T) { func TestCountWithQueryOption(t *testing.T) {
db := DB.New() db := DB.New()
db.Delete(User{}) db.Delete(User{})

View File

@ -984,6 +984,10 @@ func (scope *Scope) pluck(column string, value interface{}) *Scope {
return scope return scope
} }
if dest.Len() > 0 {
dest.Set(reflect.Zero(dest.Type()))
}
if query, ok := scope.Search.selects["query"]; !ok || !scope.isQueryForColumn(query, column) { if query, ok := scope.Search.selects["query"]; !ok || !scope.isQueryForColumn(query, column) {
scope.Search.Select(column) scope.Search.Select(column)
} }