fix:remove the tableName judgment in pluck (#4731)

This commit is contained in:
kinggo 2021-09-27 22:11:29 +08:00 committed by GitHub
parent 5202529ea1
commit 6864a24150
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -483,8 +483,6 @@ func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
column = f.DBName
}
}
} else if tx.Statement.Table == "" {
tx.AddError(ErrModelValueRequired)
}
if len(tx.Statement.Selects) != 1 {

View File

@ -31,6 +31,12 @@ func TestDistinct(t *testing.T) {
AssertEqual(t, names1, []string{"distinct", "distinct-2", "distinct-3"})
var names2 []string
DB.Scopes(func(db *gorm.DB) *gorm.DB {
return db.Table("users")
}).Where("name like ?", "distinct%").Order("name").Pluck("name", &names2)
AssertEqual(t, names2, []string{"distinct", "distinct", "distinct", "distinct-2", "distinct-3"})
var results []User
if err := DB.Distinct("name", "age").Where("name like ?", "distinct%").Order("name, age desc").Find(&results).Error; err != nil {
t.Errorf("failed to query users, got error: %v", err)