diff --git a/callbacks/associations.go b/callbacks/associations.go index 14c433c4..d78bd968 100644 --- a/callbacks/associations.go +++ b/callbacks/associations.go @@ -310,33 +310,22 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) { } } -func onConflictOption(stmt *gorm.Statement, s *schema.Schema, selectColumns map[string]bool, restricted bool, defaultUpdatingColumns []string) clause.OnConflict { - if stmt.DB.FullSaveAssociations { - defaultUpdatingColumns = make([]string, 0, len(s.DBNames)) - for _, dbName := range s.DBNames { - if v, ok := selectColumns[dbName]; (ok && !v) || (!ok && restricted) || !s.FieldsByDBName[dbName].Updatable { - continue - } - - if !s.LookUpField(dbName).PrimaryKey { - defaultUpdatingColumns = append(defaultUpdatingColumns, dbName) - } - } - } - - if len(defaultUpdatingColumns) > 0 { - columns := make([]clause.Column, 0, len(s.PrimaryFieldDBNames)) +func onConflictOption(stmt *gorm.Statement, s *schema.Schema, selectColumns map[string]bool, restricted bool, defaultUpdatingColumns []string) (onConflict clause.OnConflict) { + if len(defaultUpdatingColumns) > 0 || stmt.DB.FullSaveAssociations { + onConflict.Columns = make([]clause.Column, 0, len(s.PrimaryFieldDBNames)) for _, dbName := range s.PrimaryFieldDBNames { - columns = append(columns, clause.Column{Name: dbName}) + onConflict.Columns = append(onConflict.Columns, clause.Column{Name: dbName}) } - return clause.OnConflict{ - Columns: columns, - DoUpdates: clause.AssignmentColumns(defaultUpdatingColumns), + onConflict.UpdateAll = stmt.DB.FullSaveAssociations + if !onConflict.UpdateAll { + onConflict.DoUpdates = clause.AssignmentColumns(defaultUpdatingColumns) } + } else { + onConflict.DoNothing = true } - return clause.OnConflict{DoNothing: true} + return } func saveAssociations(db *gorm.DB, rel *schema.Relationship, values interface{}, selectColumns map[string]bool, restricted bool, defaultUpdatingColumns []string) error {