From 6864a241504bc251b249c9bd3b85c803b0df90ce Mon Sep 17 00:00:00 2001 From: kinggo <30891428+longlihale@users.noreply.github.com> Date: Mon, 27 Sep 2021 22:11:29 +0800 Subject: [PATCH] fix:remove the tableName judgment in pluck (#4731) --- finisher_api.go | 2 -- tests/distinct_test.go | 6 ++++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/finisher_api.go b/finisher_api.go index d273093f..e98efc92 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -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 { diff --git a/tests/distinct_test.go b/tests/distinct_test.go index 29a320ff..f97738a7 100644 --- a/tests/distinct_test.go +++ b/tests/distinct_test.go @@ -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)