forked from mirror/gorm
Fix comparing uncomparable type error
This commit is contained in:
parent
829158ed2f
commit
c25f26149e
11
main_test.go
11
main_test.go
|
@ -1084,7 +1084,7 @@ func TestFindOrInitialize(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestFindOrCreate(t *testing.T) {
|
||||
var user1, user2, user3, user4, user5, user6, user7 User
|
||||
var user1, user2, user3, user4, user5, user6, user7, user8 User
|
||||
db.Where(&User{Name: "find or create", Age: 33}).FirstOrCreate(&user1)
|
||||
if user1.Name != "find or create" || user1.Id == 0 || user1.Age != 33 {
|
||||
t.Errorf("user should be created with search value")
|
||||
|
@ -1124,6 +1124,15 @@ func TestFindOrCreate(t *testing.T) {
|
|||
if user7.Name != "find or create" || user7.Id == 0 || user7.Age != 44 {
|
||||
t.Errorf("user should be found and updated with assigned attrs")
|
||||
}
|
||||
|
||||
db.Where(&User{Name: "find or create embedded struct"}).Assign(User{Age: 44, CreditCard: CreditCard{Number: "1231231231"}, Emails: []Email{{Email: "jinzhu@assign_embedded_struct.com"}, {Email: "jinzhu-2@assign_embedded_struct.com"}}}).FirstOrCreate(&user8)
|
||||
if db.Where("email = ?", "jinzhu-2@assign_embedded_struct.com").First(&Email{}).RecordNotFound() {
|
||||
t.Errorf("embedded struct email should be saved")
|
||||
}
|
||||
|
||||
if db.Where("email = ?", "1231231231").First(&CreditCard{}).RecordNotFound() {
|
||||
t.Errorf("embedded struct credit card should be saved")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNot(t *testing.T) {
|
||||
|
|
|
@ -266,27 +266,34 @@ func (scope *Scope) updatedAttrsWithValues(values map[string]interface{}, ignore
|
|||
|
||||
for key, value := range values {
|
||||
if field := data.FieldByName(snakeToUpperCamel(key)); field.IsValid() {
|
||||
if field.Interface() != value {
|
||||
|
||||
switch field.Kind() {
|
||||
case reflect.Int, reflect.Int32, reflect.Int64:
|
||||
if s, ok := value.(string); ok {
|
||||
i, err := strconv.Atoi(s)
|
||||
if scope.Err(err) == nil {
|
||||
value = i
|
||||
}
|
||||
}
|
||||
|
||||
scope.db.log(field.Int() != reflect.ValueOf(value).Int())
|
||||
if field.Int() != reflect.ValueOf(value).Int() {
|
||||
func() {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
hasUpdate = true
|
||||
setFieldValue(field, value)
|
||||
}
|
||||
}()
|
||||
|
||||
if field.Interface() != value {
|
||||
switch field.Kind() {
|
||||
case reflect.Int, reflect.Int32, reflect.Int64:
|
||||
if s, ok := value.(string); ok {
|
||||
i, err := strconv.Atoi(s)
|
||||
if scope.Err(err) == nil {
|
||||
value = i
|
||||
}
|
||||
}
|
||||
|
||||
if field.Int() != reflect.ValueOf(value).Int() {
|
||||
hasUpdate = true
|
||||
setFieldValue(field, value)
|
||||
}
|
||||
default:
|
||||
hasUpdate = true
|
||||
setFieldValue(field, value)
|
||||
}
|
||||
default:
|
||||
hasUpdate = true
|
||||
setFieldValue(field, value)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue