Fix group by with count logic

This commit is contained in:
Jinzhu 2022-11-21 11:10:56 +08:00
parent 342310fba4
commit f91313436a
3 changed files with 7 additions and 7 deletions

View File

@ -465,7 +465,7 @@ func (db *DB) Count(count *int64) (tx *DB) {
tx.Statement.Dest = count tx.Statement.Dest = count
tx = tx.callbacks.Query().Execute(tx) tx = tx.callbacks.Query().Execute(tx)
if _, ok := db.Statement.Clauses["GROUP BY"]; ok || tx.RowsAffected != 1 { if tx.RowsAffected != 1 {
*count = tx.RowsAffected *count = tx.RowsAffected
} }

View File

@ -141,7 +141,7 @@ func TestCount(t *testing.T) {
} }
DB.Create(sameUsers) DB.Create(sameUsers)
if err := DB.Model(&User{}).Where("name = ?", "count-4").Group("name").Count(&count11).Error; err != nil || count11 != 1 { if err := DB.Model(&User{}).Where("name = ?", "count-4").Group("name").Count(&count11).Error; err != nil || count11 != int64(len(sameUsers)) {
t.Fatalf("Count should be 3, but got count: %v err %v", count11, err) t.Fatalf("Count should be 3, but got count: %v err %v", count11, err)
} }

View File

@ -6,13 +6,13 @@ require (
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/jinzhu/now v1.1.5 github.com/jinzhu/now v1.1.5
github.com/lib/pq v1.10.7 github.com/lib/pq v1.10.7
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a // indirect github.com/mattn/go-sqlite3 v1.14.16 // indirect
golang.org/x/text v0.4.0 // indirect golang.org/x/crypto v0.3.0 // indirect
gorm.io/driver/mysql v1.4.3 gorm.io/driver/mysql v1.4.4
gorm.io/driver/postgres v1.4.4 gorm.io/driver/postgres v1.4.5
gorm.io/driver/sqlite v1.4.3 gorm.io/driver/sqlite v1.4.3
gorm.io/driver/sqlserver v1.4.1 gorm.io/driver/sqlserver v1.4.1
gorm.io/gorm v1.24.0 gorm.io/gorm v1.24.2
) )
replace gorm.io/gorm => ../ replace gorm.io/gorm => ../