From b23c3b290e98d005cdc13e574d4a7e36045693dd Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 8 Nov 2021 18:49:49 +0800 Subject: [PATCH] Don't query with primary key when using Save --- callbacks.go | 8 +++++--- finisher_api.go | 2 +- logger/logger.go | 1 - statement.go | 4 ++++ 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/callbacks.go b/callbacks.go index 7ab38926..f344649e 100644 --- a/callbacks.go +++ b/callbacks.go @@ -130,9 +130,11 @@ func (p *processor) Execute(db *DB) *DB { f(db) } - db.Logger.Trace(stmt.Context, curTime, func() (string, int64) { - return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...), db.RowsAffected - }, db.Error) + if stmt.SQL.Len() > 0 { + db.Logger.Trace(stmt.Context, curTime, func() (string, int64) { + return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...), db.RowsAffected + }, db.Error) + } if !stmt.DB.DryRun { stmt.SQL.Reset() diff --git a/finisher_api.go b/finisher_api.go index efdbd563..920ea739 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -101,7 +101,7 @@ func (db *DB) Save(value interface{}) (tx *DB) { if tx.Error == nil && tx.RowsAffected == 0 && !tx.DryRun && !selectedUpdate { result := reflect.New(tx.Statement.Schema.ModelType).Interface() - if err := tx.Session(&Session{}).First(result).Error; errors.Is(err, ErrRecordNotFound) { + if err := tx.Session(&Session{}).Take(result).Error; errors.Is(err, ErrRecordNotFound) { return tx.Create(value) } } diff --git a/logger/logger.go b/logger/logger.go index 69d41113..0c4ca4a0 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -140,7 +140,6 @@ func (l logger) Error(ctx context.Context, msg string, data ...interface{}) { // Trace print sql message func (l logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { - if l.LogLevel <= Silent { return } diff --git a/statement.go b/statement.go index 85432e48..1bd6c2b2 100644 --- a/statement.go +++ b/statement.go @@ -665,6 +665,10 @@ func (stmt *Statement) SelectAndOmitColumns(requireCreate, requireUpdate bool) ( for _, omit := range stmt.Omits { if stmt.Schema == nil { results[omit] = false + } else if omit == "*" { + for _, dbName := range stmt.Schema.DBNames { + results[dbName] = false + } } else if omit == clause.Associations { for _, rel := range stmt.Schema.Relationships.Relations { results[rel.Name] = false