Skip to content

Commit

Permalink
Fix Pluck with Table only
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jun 9, 2020
1 parent 05e6a65 commit 22ff837
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
16 changes: 8 additions & 8 deletions finisher_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,16 @@ func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
column = f.DBName
}
}

tx.Statement.AddClauseIfNotExists(clause.Select{
Distinct: tx.Statement.Distinct,
Columns: []clause.Column{{Name: column}},
})
tx.Statement.Dest = dest
tx.callbacks.Query().Execute(tx)
} else {
} else if tx.Statement.Table == "" {
tx.AddError(ErrorModelValueRequired)
}

tx.Statement.AddClauseIfNotExists(clause.Select{
Distinct: tx.Statement.Distinct,
Columns: []clause.Column{{Name: column}},
})
tx.Statement.Dest = dest
tx.callbacks.Query().Execute(tx)
return
}

Expand Down
32 changes: 17 additions & 15 deletions scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,26 @@ func Scan(rows *sql.Rows, db *DB, initialized bool) {

db.Statement.ReflectValue.Set(reflect.MakeSlice(db.Statement.ReflectValue.Type(), 0, 0))

for idx, column := range columns {
if field := db.Statement.Schema.LookUpField(column); field != nil && field.Readable {
fields[idx] = field
} else if names := strings.Split(column, "__"); len(names) > 1 {
if len(joinFields) == 0 {
joinFields = make([][2]*schema.Field, len(columns))
}
if db.Statement.Schema != nil {
for idx, column := range columns {
if field := db.Statement.Schema.LookUpField(column); field != nil && field.Readable {
fields[idx] = field
} else if names := strings.Split(column, "__"); len(names) > 1 {
if len(joinFields) == 0 {
joinFields = make([][2]*schema.Field, len(columns))
}

if rel, ok := db.Statement.Schema.Relationships.Relations[names[0]]; ok {
if field := rel.FieldSchema.LookUpField(strings.Join(names[1:], "__")); field != nil && field.Readable {
fields[idx] = field
joinFields[idx] = [2]*schema.Field{rel.Field, field}
continue
if rel, ok := db.Statement.Schema.Relationships.Relations[names[0]]; ok {
if field := rel.FieldSchema.LookUpField(strings.Join(names[1:], "__")); field != nil && field.Readable {
fields[idx] = field
joinFields[idx] = [2]*schema.Field{rel.Field, field}
continue
}
}
values[idx] = &sql.RawBytes{}
} else {
values[idx] = &sql.RawBytes{}
}
values[idx] = &sql.RawBytes{}
} else {
values[idx] = &sql.RawBytes{}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/distinct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 22ff837

Please sign in to comment.