diff --git a/finisher_api.go b/finisher_api.go index 34e1596b..741a9456 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -399,7 +399,7 @@ func (db *DB) Count(count *int64) (tx *DB) { if tx.Statement.Distinct { expr = clause.Expr{SQL: "COUNT(DISTINCT(?))", Vars: []interface{}{clause.Column{Name: dbName}}} - } else { + } else if dbName != "*" { expr = clause.Expr{SQL: "COUNT(?)", Vars: []interface{}{clause.Column{Name: dbName}}} } } diff --git a/tests/count_test.go b/tests/count_test.go index dd25f8b6..de06d0eb 100644 --- a/tests/count_test.go +++ b/tests/count_test.go @@ -112,7 +112,7 @@ func TestCount(t *testing.T) { if err := DB.Model(&User{}).Where("name in ?", []string{user1.Name, user2.Name, user3.Name}).Select( "(CASE WHEN age=18 THEN 1 ELSE 2 END) as age", "name", ).Count(&count8).Find(&users).Error; err != nil || count8 != 3 { - t.Fatalf(fmt.Sprintf("Count should work, but got err %v", err)) + t.Fatalf("Count should work, but got err %v", err) } expects = []User{User{Name: "count-1", Age: 1}, {Name: "count-2", Age: 1}, {Name: "count-3", Age: 1}} @@ -123,9 +123,15 @@ func TestCount(t *testing.T) { AssertEqual(t, users, expects) var count9 int64 - if err := DB.Debug().Scopes(func(tx *gorm.DB) *gorm.DB { + if err := DB.Scopes(func(tx *gorm.DB) *gorm.DB { return tx.Table("users") }).Where("name in ?", []string{user1.Name, user2.Name, user3.Name}).Count(&count9).Find(&users).Error; err != nil || count9 != 3 { - t.Fatalf(fmt.Sprintf("Count should work, but got err %v", err)) + t.Fatalf("Count should work, but got err %v", err) } + + var count10 int64 + if err := DB.Model(&User{}).Select("*").Where("name in ?", []string{user1.Name, user2.Name, user3.Name}).Count(&count10).Error; err != nil || count10 != 3 { + t.Fatalf("Count should be 3, but got count: %v err %v", count10, err) + } + }