Don't query with primary key when using Save

This commit is contained in:
Jinzhu 2021-11-08 18:49:49 +08:00
parent d9d5c4dce0
commit b23c3b290e
4 changed files with 10 additions and 5 deletions

View File

@ -130,9 +130,11 @@ func (p *processor) Execute(db *DB) *DB {
f(db) f(db)
} }
db.Logger.Trace(stmt.Context, curTime, func() (string, int64) { if stmt.SQL.Len() > 0 {
return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...), db.RowsAffected db.Logger.Trace(stmt.Context, curTime, func() (string, int64) {
}, db.Error) return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...), db.RowsAffected
}, db.Error)
}
if !stmt.DB.DryRun { if !stmt.DB.DryRun {
stmt.SQL.Reset() stmt.SQL.Reset()

View File

@ -101,7 +101,7 @@ func (db *DB) Save(value interface{}) (tx *DB) {
if tx.Error == nil && tx.RowsAffected == 0 && !tx.DryRun && !selectedUpdate { if tx.Error == nil && tx.RowsAffected == 0 && !tx.DryRun && !selectedUpdate {
result := reflect.New(tx.Statement.Schema.ModelType).Interface() 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) return tx.Create(value)
} }
} }

View File

@ -140,7 +140,6 @@ func (l logger) Error(ctx context.Context, msg string, data ...interface{}) {
// Trace print sql message // Trace print sql message
func (l logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { func (l logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
if l.LogLevel <= Silent { if l.LogLevel <= Silent {
return return
} }

View File

@ -665,6 +665,10 @@ func (stmt *Statement) SelectAndOmitColumns(requireCreate, requireUpdate bool) (
for _, omit := range stmt.Omits { for _, omit := range stmt.Omits {
if stmt.Schema == nil { if stmt.Schema == nil {
results[omit] = false results[omit] = false
} else if omit == "*" {
for _, dbName := range stmt.Schema.DBNames {
results[dbName] = false
}
} else if omit == clause.Associations { } else if omit == clause.Associations {
for _, rel := range stmt.Schema.Relationships.Relations { for _, rel := range stmt.Schema.Relationships.Relations {
results[rel.Name] = false results[rel.Name] = false