forked from mirror/gorm
Don't query with primary key when using Save
This commit is contained in:
parent
d9d5c4dce0
commit
b23c3b290e
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue