mirror of https://github.com/go-gorm/gorm.git
Fix Pluck with Table only
This commit is contained in:
parent
05e6a65ee1
commit
22ff8377df
|
@ -289,6 +289,9 @@ func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
|
|||
column = f.DBName
|
||||
}
|
||||
}
|
||||
} else if tx.Statement.Table == "" {
|
||||
tx.AddError(ErrorModelValueRequired)
|
||||
}
|
||||
|
||||
tx.Statement.AddClauseIfNotExists(clause.Select{
|
||||
Distinct: tx.Statement.Distinct,
|
||||
|
@ -296,9 +299,6 @@ func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
|
|||
})
|
||||
tx.Statement.Dest = dest
|
||||
tx.callbacks.Query().Execute(tx)
|
||||
} else {
|
||||
tx.AddError(ErrorModelValueRequired)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
2
scan.go
2
scan.go
|
@ -84,6 +84,7 @@ func Scan(rows *sql.Rows, db *DB, initialized bool) {
|
|||
|
||||
db.Statement.ReflectValue.Set(reflect.MakeSlice(db.Statement.ReflectValue.Type(), 0, 0))
|
||||
|
||||
if db.Statement.Schema != nil {
|
||||
for idx, column := range columns {
|
||||
if field := db.Statement.Schema.LookUpField(column); field != nil && field.Readable {
|
||||
fields[idx] = field
|
||||
|
@ -104,6 +105,7 @@ func Scan(rows *sql.Rows, db *DB, initialized bool) {
|
|||
values[idx] = &sql.RawBytes{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pluck values into slice of data
|
||||
isPluck := len(fields) == 1 && reflectValueType.Kind() != reflect.Struct
|
||||
|
|
|
@ -21,7 +21,7 @@ func TestDistinct(t *testing.T) {
|
|||
}
|
||||
|
||||
var names []string
|
||||
DB.Model(&User{}).Where("name like ?", "distinct%").Order("name").Pluck("Name", &names)
|
||||
DB.Table("users").Where("name like ?", "distinct%").Order("name").Pluck("name", &names)
|
||||
AssertEqual(t, names, []string{"distinct", "distinct", "distinct", "distinct-2", "distinct-3"})
|
||||
|
||||
var names1 []string
|
||||
|
|
Loading…
Reference in New Issue