Skip to content

Commit

Permalink
Add example for broken nested where
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Biel committed Feb 12, 2024
1 parent 4f84925 commit a576685
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
74 changes: 69 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,80 @@ import (
)

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

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
//manager1 := User{Name: "manager1", Company: Company{Name: "company1", Country: "DE"}}
//DB.Create(&manager1)
//manager2 := User{Name: "manager2", Company: Company{Name: "company2", Country: "US"}}
//DB.Create(&manager2)
user1 := User{Name: "user1",
Something: "1",
Company: Company{Name: "company1", Country: "DE"},
Pets: []*Pet{
{Name: "dog1"},
{Name: "dog2"},
{Name: "dog3"},
{Name: "dog4"},
}}
DB.Create(&user1)
user11 := User{Name: "user11",
Something: "1",
Company: Company{Name: "company1", Country: "GB"},
Pets: []*Pet{
{Name: "dog1"},
{Name: "dog22"},
{Name: "dog33"},
{Name: "dog4"},
}}
DB.Create(&user11)
user2 := User{Name: "user2",
Something: "2",
Company: Company{Name: "company2", Country: "US"},
Pets: []*Pet{
{Name: "dog4"},
{Name: "dog6"},
{Name: "dog7"},
{Name: "dog8"},
}}
DB.Create(&user2)
user22 := User{Name: "user22",
Something: "2",
Company: Company{Name: "company2", Country: "DE"},
Pets: []*Pet{
{Name: "dog4"},
{Name: "dog66"},
{Name: "dog77"},
{Name: "dog8"},
}}
DB.Create(&user22)

DB.Create(&user)
query := DB.Table("users").
Select("companies.country", "COUNT(DISTINCT users.name)").
Joins("LEFT JOIN companies ON users.company_id = companies.id").
Group("companies.country")
query.Where(query.Where("users.name ILIKE ?", "%%").
Or("companies.name ILIKE ?", "%%"))
query.Where("users.something IN (?)", []string{"1"})

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
type Result struct {
Country string
Count int
}

var result []Result
if err := query.Find(&result).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
t.Logf("Result: %v", result)
if len(result) != 2 {
t.Errorf("Failed, unexpected length if results")
}
if result[0].Country != "DE" || result[0].Count != 1 {
t.Errorf("Failed, not correct DE entry")
}
if result[1].Country != "GB" || result[1].Count != 1 {
t.Errorf("Failed, not correct GB entry")
}
}
6 changes: 4 additions & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type User struct {
Languages []Language `gorm:"many2many:UserSpeak"`
Friends []*User `gorm:"many2many:user_friends"`
Active bool
Something string
}

type Account struct {
Expand All @@ -50,8 +51,9 @@ type Toy struct {
}

type Company struct {
ID int
Name string
ID int
Name string
Country string
}

type Language struct {
Expand Down

0 comments on commit a576685

Please sign in to comment.