Fix comparing uncomparable type error

This commit is contained in:
Jinzhu 2014-01-30 16:41:10 +08:00
parent 829158ed2f
commit c25f26149e
2 changed files with 34 additions and 18 deletions

View File

@ -1084,7 +1084,7 @@ func TestFindOrInitialize(t *testing.T) {
} }
func TestFindOrCreate(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) db.Where(&User{Name: "find or create", Age: 33}).FirstOrCreate(&user1)
if user1.Name != "find or create" || user1.Id == 0 || user1.Age != 33 { if user1.Name != "find or create" || user1.Id == 0 || user1.Age != 33 {
t.Errorf("user should be created with search value") 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 { if user7.Name != "find or create" || user7.Id == 0 || user7.Age != 44 {
t.Errorf("user should be found and updated with assigned attrs") 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) { func TestNot(t *testing.T) {

View File

@ -266,8 +266,15 @@ func (scope *Scope) updatedAttrsWithValues(values map[string]interface{}, ignore
for key, value := range values { for key, value := range values {
if field := data.FieldByName(snakeToUpperCamel(key)); field.IsValid() { if field := data.FieldByName(snakeToUpperCamel(key)); field.IsValid() {
if field.Interface() != value { func() {
defer func() {
if err := recover(); err != nil {
hasUpdate = true
setFieldValue(field, value)
}
}()
if field.Interface() != value {
switch field.Kind() { switch field.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int32, reflect.Int64:
if s, ok := value.(string); ok { if s, ok := value.(string); ok {
@ -277,7 +284,6 @@ func (scope *Scope) updatedAttrsWithValues(values map[string]interface{}, ignore
} }
} }
scope.db.log(field.Int() != reflect.ValueOf(value).Int())
if field.Int() != reflect.ValueOf(value).Int() { if field.Int() != reflect.ValueOf(value).Int() {
hasUpdate = true hasUpdate = true
setFieldValue(field, value) setFieldValue(field, value)
@ -287,6 +293,7 @@ func (scope *Scope) updatedAttrsWithValues(values map[string]interface{}, ignore
setFieldValue(field, value) setFieldValue(field, value)
} }
} }
}()
} }
} }
return return