Skip to content

Commit

Permalink
fix: omit not work when use join (go-gorm#5034)
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou authored Jan 28, 2022
1 parent 98c4b78 commit c0bea44
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion callbacks/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func BuildQuerySQL(db *gorm.DB) {
}

if len(db.Statement.Joins) != 0 || len(joins) != 0 {
if len(db.Statement.Selects) == 0 && db.Statement.Schema != nil {
if len(db.Statement.Selects) == 0 && len(db.Statement.Omits) == 0 && db.Statement.Schema != nil {
clauseSelect.Columns = make([]clause.Column, len(db.Statement.Schema.DBNames))
for idx, dbName := range db.Statement.Schema.DBNames {
clauseSelect.Columns[idx] = clause.Column{Table: db.Statement.Table, Name: dbName}
Expand Down
3 changes: 1 addition & 2 deletions tests/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestWithSingleConnection(t *testing.T) {
var expectedName = "test"
expectedName := "test"
var actualName string

setSQL, getSQL := getSetSQL(DB.Dialector.Name())
Expand All @@ -27,7 +27,6 @@ func TestWithSingleConnection(t *testing.T) {
}
return nil
})

if err != nil {
t.Errorf(fmt.Sprintf("WithSingleConnection should work, but got err %v", err))
}
Expand Down
16 changes: 16 additions & 0 deletions tests/joins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ func TestJoinsWithSelect(t *testing.T) {
}
}

func TestJoinWithOmit(t *testing.T) {
user := *GetUser("joins_with_omit", Config{Pets: 2})
DB.Save(&user)

results := make([]*User, 0)

if err := DB.Table("users").Omit("name").Where("users.name = ?", "joins_with_omit").Joins("left join pets on pets.user_id = users.id").Find(&results).Error; err != nil {
return
}

if len(results) != 2 || results[0].Name != "" || results[1].Name != "" {
t.Errorf("Should find all two pets with Join omit and should not find user's name, got %+v", results)
return
}
}

func TestJoinCount(t *testing.T) {
companyA := Company{Name: "A"}
companyB := Company{Name: "B"}
Expand Down

0 comments on commit c0bea44

Please sign in to comment.