Skip to content

Commit

Permalink
#6790: Count() erases Select of preloads
Browse files Browse the repository at this point in the history
  • Loading branch information
dschreij committed Jan 10, 2024
1 parent e5208c4 commit 99f8560
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 13 deletions.
24 changes: 13 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ module gorm.io/playground
go 1.20

require (
gorm.io/driver/mysql v1.5.1
gorm.io/driver/postgres v1.5.2
gorm.io/driver/sqlite v1.5.3
gorm.io/driver/sqlserver v1.5.1
gorm.io/gorm v1.25.4
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.4
gorm.io/driver/sqlite v1.5.4
gorm.io/driver/sqlserver v1.5.2
gorm.io/gorm v1.25.5
)

require (
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgx/v5 v5.5.1 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.5.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/text v0.12.0 // indirect
github.com/mattn/go-sqlite3 v1.14.19 // indirect
github.com/microsoft/go-mssqldb v1.6.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

replace gorm.io/gorm => ./gorm
78 changes: 76 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,89 @@ import (

// GORM_REPO: https://github.com/go-gorm/gorm.git
// GORM_BRANCH: master
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver
// TEST_DRIVERS: postgres

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}

DB.Create(&user)
tx := DB.Begin()
tx.Create(&user)

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
tx.Rollback()
}

func TestJoinsWithoutCount(t *testing.T) {
users := []User{
{
Name: "John Doe",
Company: Company{
Name: "Company A",
},
},
{
Name: "Jane Doe",
Company: Company{
Name: "Company B",
},
},
}
tx := DB.Begin()
tx.Create(&users)

tx.Model(&User{}).Joins("Company").Order("\"Company\".\"name\" ASC")
var result []User
if err := tx.Find(&result).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
if len(result) != 2 {
t.Errorf("Failed, expected 2 companies, got %d", len(result))
}
if result[0].Company.Name != "Company A" {
t.Errorf("Failed, expected Company A, got %s", result[0].Company.Name)
}
tx.Rollback()
}

func TestJoinsWithCount(t *testing.T) {
users := []User{
{
Name: "John Doe",
Company: Company{
Name: "Company A",
},
},
{
Name: "Jane Doe",
Company: Company{
Name: "Company B",
},
},
}
tx := DB.Begin()
tx.Create(&users)
tx.Model(&User{}).Joins("Company").
Order("\"Company\".\"name\" ASC") // Order still works!

// --- Critical change
var count int64
if err := tx.Count(&count).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
// --- End critical change

var result []User
if err := tx.Find(&result).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
if len(result) != 2 {
t.Errorf("Failed, expected 2 companies, got %d", len(result))
}
if result[0].Company.Name != "Company A" {
t.Errorf("Failed, expected Company A, got %s", result[0].Company.Name)
}
tx.Rollback()
}

0 comments on commit 99f8560

Please sign in to comment.