Skip to content

Commit

Permalink
Don't query with primary key when using Save
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Nov 8, 2021
1 parent d9d5c4d commit b23c3b2
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
8 changes: 5 additions & 3 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion finisher_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
1 change: 0 additions & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b23c3b2

Please sign in to comment.