Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jun 15, 2020
1 parent 7ea143b commit 7a6ff9f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
6 changes: 6 additions & 0 deletions main_test.go
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"reflect"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -843,6 +844,11 @@ func TestJoinsWithSelect(t *testing.T) {

var results []result
DB.Table("users").Select("name, emails.email").Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins_with_select").Scan(&results)

sort.Slice(results, func(i, j int) bool {
return strings.Compare(results[i].Email, results[j].Email) < 0
})

if len(results) != 2 || results[0].Email != "join1@example.com" || results[1].Email != "join2@example.com" {
t.Errorf("Should find all two emails with Join select")
}
Expand Down
26 changes: 15 additions & 11 deletions scope.go
Expand Up @@ -913,21 +913,25 @@ func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[strin
results = map[string]interface{}{}

for key, value := range convertInterfaceToMap(value, true, scope.db) {
if field, ok := scope.FieldByName(key); ok && scope.changeableField(field) {
if _, ok := value.(*SqlExpr); ok {
hasUpdate = true
results[field.DBName] = value
} else {
err := field.Set(value)
if field.IsNormal && !field.IsIgnored {
if field, ok := scope.FieldByName(key); ok {
if scope.changeableField(field) {
if _, ok := value.(*SqlExpr); ok {
hasUpdate = true
if err == ErrUnaddressable {
results[field.DBName] = value
} else {
results[field.DBName] = field.Field.Interface()
results[field.DBName] = value
} else {
err := field.Set(value)
if field.IsNormal && !field.IsIgnored {
hasUpdate = true
if err == ErrUnaddressable {
results[field.DBName] = value
} else {
results[field.DBName] = field.Field.Interface()
}
}
}
}
} else {
results[key] = value
}
}
return
Expand Down

0 comments on commit 7a6ff9f

Please sign in to comment.