mirror of https://github.com/go-gorm/gorm.git
Fix extra updates when Append associations for many2many
This commit is contained in:
parent
048963c568
commit
a480621b76
|
@ -30,19 +30,44 @@ func (association *Association) Append(values ...interface{}) *Association {
|
||||||
scope := association.Scope
|
scope := association.Scope
|
||||||
field := association.Field
|
field := association.Field
|
||||||
|
|
||||||
|
createJoinTable := func(reflectValue reflect.Value) {
|
||||||
|
var value = reflectValue.Interface()
|
||||||
|
if reflectValue.Kind() != reflect.Ptr {
|
||||||
|
reflectPtr := reflect.New(reflectValue.Type())
|
||||||
|
reflectPtr.Elem().Set(reflectValue)
|
||||||
|
value = reflectPtr.Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
if scope.New(value).PrimaryKeyZero() {
|
||||||
|
scope.NewDB().Save(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
relationship := association.Field.Relationship
|
||||||
|
association.setErr(relationship.JoinTableHandler.Add(relationship.JoinTableHandler, scope.NewDB(), scope.Value, value))
|
||||||
|
|
||||||
|
result := reflect.ValueOf(value)
|
||||||
|
fieldElemType := field.Field.Type().Elem()
|
||||||
|
if result.Type().AssignableTo(fieldElemType) {
|
||||||
|
field.Set(reflect.Append(field.Field, result))
|
||||||
|
} else if result.Type().Elem().AssignableTo(fieldElemType) {
|
||||||
|
field.Set(reflect.Append(field.Field, result.Elem()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
reflectvalue := reflect.Indirect(reflect.ValueOf(value))
|
reflectValue := reflect.Indirect(reflect.ValueOf(value))
|
||||||
if reflectvalue.Kind() == reflect.Struct {
|
|
||||||
field.Set(reflect.Append(field.Field, reflectvalue))
|
if reflectValue.Kind() == reflect.Struct {
|
||||||
} else if reflectvalue.Kind() == reflect.Slice {
|
createJoinTable(reflectValue)
|
||||||
field.Set(reflect.AppendSlice(field.Field, reflectvalue))
|
} else if reflectValue.Kind() == reflect.Slice {
|
||||||
|
for i := 0; i < reflectValue.Len(); i++ {
|
||||||
|
createJoinTable(reflectValue.Index(i))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
association.setErr(errors.New("invalid association type"))
|
association.setErr(errors.New("invalid association type"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
scope.Search.Select(association.Column)
|
return association
|
||||||
scope.callCallbacks(scope.db.parent.callback.updates)
|
|
||||||
return association.setErr(scope.db.Error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (association *Association) Delete(values ...interface{}) *Association {
|
func (association *Association) Delete(values ...interface{}) *Association {
|
||||||
|
@ -230,7 +255,7 @@ func toQueryMarks(primaryValues [][]interface{}) string {
|
||||||
|
|
||||||
for _, primaryValue := range primaryValues {
|
for _, primaryValue := range primaryValues {
|
||||||
var marks []string
|
var marks []string
|
||||||
for _,_ = range primaryValue {
|
for _, _ = range primaryValue {
|
||||||
marks = append(marks, "?")
|
marks = append(marks, "?")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -160,7 +160,7 @@ func TestManyToMany(t *testing.T) {
|
||||||
|
|
||||||
languageA := Language{Name: "AA"}
|
languageA := Language{Name: "AA"}
|
||||||
DB.Save(&languageA)
|
DB.Save(&languageA)
|
||||||
DB.Model(&User{Id: user.Id}).Association("Languages").Append(languageA)
|
DB.Model(&User{Id: user.Id}).Association("Languages").Append(&languageA)
|
||||||
|
|
||||||
languageC := Language{Name: "CC"}
|
languageC := Language{Name: "CC"}
|
||||||
DB.Save(&languageC)
|
DB.Save(&languageC)
|
||||||
|
|
Loading…
Reference in New Issue