mirror of https://github.com/go-gorm/gorm.git
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)
|
f(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if stmt.SQL.Len() > 0 {
|
||||||
db.Logger.Trace(stmt.Context, curTime, func() (string, int64) {
|
db.Logger.Trace(stmt.Context, curTime, func() (string, int64) {
|
||||||
return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...), db.RowsAffected
|
return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...), db.RowsAffected
|
||||||
}, db.Error)
|
}, db.Error)
|
||||||
|
}
|
||||||
|
|
||||||
if !stmt.DB.DryRun {
|
if !stmt.DB.DryRun {
|
||||||
stmt.SQL.Reset()
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue