From 43dc867644b879f8f87fd0598ac0b459232d9293 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 10 Feb 2018 15:16:20 +0800 Subject: [PATCH] Allow save association relations w/o saving association --- association_test.go | 2 +- callback_save.go | 31 ++++++++++++++++++------------- scope.go | 2 +- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/association_test.go b/association_test.go index f37047d1..34822dbc 100644 --- a/association_test.go +++ b/association_test.go @@ -909,7 +909,7 @@ func TestSkipSaveAssociation(t *testing.T) { company := Company{Name: "skip_save_association"} DB.Save(&company) company.Name = "skip_save_association_modified" - user := User{Name: "jinzhu", CompanyID: company.ID, Company: company} + user := User{Name: "jinzhu", Company: company} DB.Save(&user) if !DB.Where("name = ?", "skip_save_association_modified").First(&Company{}).RecordNotFound() { diff --git a/callback_save.go b/callback_save.go index fa32c907..544354d0 100644 --- a/callback_save.go +++ b/callback_save.go @@ -12,22 +12,25 @@ func commitOrRollbackTransactionCallback(scope *Scope) { func saveFieldAsAssociation(scope *Scope, field *Field) (bool, *Relationship) { if scope.changeableField(field) && !field.IsBlank && !field.IsIgnored { - if value, ok := field.TagSettings["SAVE_ASSOCIATIONS"]; !ok || (value != "false" && value != "skip") { - return true, field.Relationship + if field.Relationship != nil { + if value, ok := field.TagSettings["SAVE_ASSOCIATIONS"]; (!ok || (value != "false" && value != "skip")) && scope.allowSaveAssociations() { + return true, field.Relationship + } + return false, field.Relationship } - return false, field.Relationship } return false, nil } func saveBeforeAssociationsCallback(scope *Scope) { for _, field := range scope.Fields() { - ok, relationship := saveFieldAsAssociation(scope, field) - if relationship != nil && relationship.Kind == "belongs_to" { + if allowSaveAssociation, relationship := saveFieldAsAssociation(scope, field); relationship != nil && relationship.Kind == "belongs_to" { fieldValue := field.Field.Addr().Interface() - if ok && scope.shouldSaveAssociations() { + + if allowSaveAssociation { scope.Err(scope.NewDB().Save(fieldValue).Error) } + if len(relationship.ForeignFieldNames) != 0 { // set value's foreign key for idx, fieldName := range relationship.ForeignFieldNames { @@ -42,11 +45,8 @@ func saveBeforeAssociationsCallback(scope *Scope) { } func saveAfterAssociationsCallback(scope *Scope) { - if !scope.shouldSaveAssociations() { - return - } for _, field := range scope.Fields() { - if ok, relationship := saveFieldAsAssociation(scope, field); ok && relationship != nil && + if allowSaveAssociation, relationship := saveFieldAsAssociation(scope, field); relationship != nil && (relationship.Kind == "has_one" || relationship.Kind == "has_many" || relationship.Kind == "many_to_many") { value := field.Field @@ -70,9 +70,11 @@ func saveAfterAssociationsCallback(scope *Scope) { scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue)) } - scope.Err(newDB.Save(elem).Error) + if allowSaveAssociation { + scope.Err(newDB.Save(elem).Error) + } - if joinTableHandler := relationship.JoinTableHandler; joinTableHandler != nil { + if joinTableHandler := relationship.JoinTableHandler; joinTableHandler != nil && !newScope.PrimaryKeyZero() { scope.Err(joinTableHandler.Add(joinTableHandler, newDB, scope.Value, newScope.Value)) } } @@ -91,7 +93,10 @@ func saveAfterAssociationsCallback(scope *Scope) { if relationship.PolymorphicType != "" { scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue)) } - scope.Err(scope.NewDB().Save(elem).Error) + + if allowSaveAssociation { + scope.Err(scope.NewDB().Save(elem).Error) + } } } } diff --git a/scope.go b/scope.go index a10cb3a2..9ae33913 100644 --- a/scope.go +++ b/scope.go @@ -993,7 +993,7 @@ func (scope *Scope) changeableField(field *Field) bool { return true } -func (scope *Scope) shouldSaveAssociations() bool { +func (scope *Scope) allowSaveAssociations() bool { if saveAssociations, ok := scope.Get("gorm:save_associations"); ok { if v, ok := saveAssociations.(bool); ok && !v { return false