From 7598204dc3b0439196b66505e2a7acdd0537ea31 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 29 Jan 2021 16:40:07 +0800 Subject: [PATCH] Support `FullSaveAssociations` for association mode, close #4010 --- association.go | 14 ++++++++++++-- gorm.go | 1 - 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/association.go b/association.go index 4c55c7e1..3a2942fd 100644 --- a/association.go +++ b/association.go @@ -385,7 +385,9 @@ func (association *Association) saveAssociation(clear bool, values ...interface{ for name, ok := range selectColumns { columnName := "" if strings.HasPrefix(name, association.Relationship.Name) { - columnName = strings.TrimPrefix(name, association.Relationship.Name) + if columnName = strings.TrimPrefix(name, association.Relationship.Name); columnName == ".*" { + columnName = name + } } else if strings.HasPrefix(name, clause.Associations) { columnName = name } @@ -404,7 +406,15 @@ func (association *Association) saveAssociation(clear bool, values ...interface{ selectedSaveColumns = append(selectedSaveColumns, ref.ForeignKey.Name) } } - associationDB := association.DB.Session(&Session{}).Model(nil).Select(selectedSaveColumns).Session(&Session{}) + + associationDB := association.DB.Session(&Session{}).Model(nil) + if !association.DB.FullSaveAssociations { + associationDB.Select(selectedSaveColumns) + } + if len(omitColumns) > 0 { + associationDB.Omit(omitColumns...) + } + associationDB = associationDB.Session(&Session{}) switch reflectValue.Kind() { case reflect.Slice, reflect.Array: diff --git a/gorm.go b/gorm.go index 88885407..1109e8cd 100644 --- a/gorm.go +++ b/gorm.go @@ -167,7 +167,6 @@ func (db *DB) Session(config *Session) *DB { clone: 1, } ) - if config.CreateBatchSize > 0 { tx.Config.CreateBatchSize = config.CreateBatchSize }