fix: omit not work when use join (#5034)

This commit is contained in:
li-jin-gou 2022-01-28 22:16:42 +08:00 committed by GitHub
parent 98c4b78e4d
commit c0bea447b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View File

@ -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}

View File

@ -9,7 +9,7 @@ import (
)
func TestWithSingleConnection(t *testing.T) {
var expectedName = "test"
expectedName := "test"
var actualName string
setSQL, getSQL := getSetSQL(DB.Dialector.Name())
@ -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))
}

View File

@ -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"}