From 22ff8377dfaf208c1db8cb4923a481990f7e76a5 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 9 Jun 2020 15:34:55 +0800 Subject: [PATCH] Fix Pluck with Table only --- finisher_api.go | 16 ++++++++-------- scan.go | 34 ++++++++++++++++++---------------- tests/distinct_test.go | 2 +- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/finisher_api.go b/finisher_api.go index fc21e490..d45c6c4f 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -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 } diff --git a/scan.go b/scan.go index f1cdb2e5..1f0aacd0 100644 --- a/scan.go +++ b/scan.go @@ -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 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 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 + } + } + values[idx] = &sql.RawBytes{} + } else { + values[idx] = &sql.RawBytes{} } - values[idx] = &sql.RawBytes{} - } else { - values[idx] = &sql.RawBytes{} } } diff --git a/tests/distinct_test.go b/tests/distinct_test.go index f5a969a8..248602d3 100644 --- a/tests/distinct_test.go +++ b/tests/distinct_test.go @@ -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