diff --git a/callbacks/create.go b/callbacks/create.go index e94b7eca..0fe1dc93 100644 --- a/callbacks/create.go +++ b/callbacks/create.go @@ -84,8 +84,10 @@ func Create(config *Config) func(db *gorm.DB) { db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars..., ) if db.AddError(err) == nil { + defer func() { + db.AddError(rows.Close()) + }() gorm.Scan(rows, db, mode) - db.AddError(rows.Close()) } return diff --git a/callbacks/query.go b/callbacks/query.go index 6ba3dd38..6eda52ef 100644 --- a/callbacks/query.go +++ b/callbacks/query.go @@ -20,8 +20,10 @@ func Query(db *gorm.DB) { db.AddError(err) return } + defer func() { + db.AddError(rows.Close()) + }() gorm.Scan(rows, db, 0) - db.AddError(rows.Close()) } } }