Fix association replace non-addressable panic
This commit is contained in:
		
							parent
							
								
									5e599a07ec
								
							
						
					
					
						commit
						17517b05ec
					
				| @ -384,6 +384,11 @@ func (association *Association) saveAssociation(clear bool, values ...interface{ | ||||
| 	) | ||||
| 
 | ||||
| 	appendToRelations := func(source, rv reflect.Value, clear bool) { | ||||
| 		if !rv.CanAddr() { | ||||
| 			association.Error = ErrInvalidValue | ||||
| 			return | ||||
| 		} | ||||
| 
 | ||||
| 		switch association.Relationship.Type { | ||||
| 		case schema.HasOne, schema.BelongsTo: | ||||
| 			switch rv.Kind() { | ||||
| @ -510,6 +515,9 @@ func (association *Association) saveAssociation(clear bool, values ...interface{ | ||||
| 
 | ||||
| 		for i := 0; i < reflectValue.Len(); i++ { | ||||
| 			appendToRelations(reflectValue.Index(i), reflect.Indirect(reflect.ValueOf(values[i])), clear) | ||||
| 			if association.Error != nil { | ||||
| 				return | ||||
| 			} | ||||
| 
 | ||||
| 			// TODO support save slice data, sql with case?
 | ||||
| 			association.Error = associationDB.Updates(reflectValue.Index(i).Addr().Interface()).Error | ||||
| @ -531,6 +539,9 @@ func (association *Association) saveAssociation(clear bool, values ...interface{ | ||||
| 		for idx, value := range values { | ||||
| 			rv := reflect.Indirect(reflect.ValueOf(value)) | ||||
| 			appendToRelations(reflectValue, rv, clear && idx == 0) | ||||
| 			if association.Error != nil { | ||||
| 				return | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		if len(values) > 0 { | ||||
|  | ||||
| @ -554,3 +554,15 @@ func TestHasManyAssociationUnscoped(t *testing.T) { | ||||
| 		t.Errorf("expected %d contents, got %d", 0, len(contents)) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestHasManyAssociationReplaceWithNonValidValue(t *testing.T) { | ||||
| 	user := User{Name: "jinzhu", Languages: []Language{{Name: "EN"}}} | ||||
| 
 | ||||
| 	if err := DB.Create(&user).Error; err != nil { | ||||
| 		t.Fatalf("errors happened when create: %v", err) | ||||
| 	} | ||||
| 
 | ||||
| 	if err := DB.Model(&user).Association("Languages").Replace(Language{Name: "DE"}, Language{Name: "FR"}); err == nil { | ||||
| 		t.Error("expected association error to be not nil") | ||||
| 	} | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 sgsv
						sgsv