From 27bb9137d3ad1751e47ceb6e325fb5d17b0eb7aa Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 18 Mar 2021 11:44:04 +0800 Subject: [PATCH] Refactor OnConflict.UpdateALl --- callbacks/create.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/callbacks/create.go b/callbacks/create.go index 10da731f..909d984a 100644 --- a/callbacks/create.go +++ b/callbacks/create.go @@ -353,15 +353,14 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) { } } - onConflict := clause.OnConflict{ - Columns: make([]clause.Column, len(stmt.Schema.PrimaryFieldDBNames)), - DoUpdates: clause.AssignmentColumns(columns), - } + onConflict.DoUpdates = clause.AssignmentColumns(columns) - for idx, field := range stmt.Schema.PrimaryFields { - onConflict.Columns[idx] = clause.Column{Name: field.DBName} + // use primary fields as default OnConflict columns + if len(onConflict.Columns) == 0 { + for _, field := range stmt.Schema.PrimaryFields { + onConflict.Columns = append(onConflict.Columns, clause.Column{Name: field.DBName}) + } } - stmt.AddClause(onConflict) } }