diff --git a/callbacks/create.go b/callbacks/create.go index c86cefe4..b41a3ef2 100644 --- a/callbacks/create.go +++ b/callbacks/create.go @@ -51,7 +51,7 @@ func Create(config *Config) func(db *gorm.DB) { db.Statement.Build("INSERT", "VALUES", "ON CONFLICT") } - if !db.DryRun { + if !db.DryRun && db.Error == nil { result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...) if err == nil { @@ -130,7 +130,7 @@ func CreateWithReturning(db *gorm.DB) { db.Statement.WriteQuoted(field.DBName) } - if !db.DryRun { + if !db.DryRun && db.Error == nil { rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...) if err == nil { @@ -179,7 +179,7 @@ func CreateWithReturning(db *gorm.DB) { db.AddError(err) } } - } else if !db.DryRun { + } else if !db.DryRun && db.Error == nil { if result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...); err == nil { db.RowsAffected, _ = result.RowsAffected() } else { diff --git a/callbacks/delete.go b/callbacks/delete.go index 51a33bf0..288f2d69 100644 --- a/callbacks/delete.go +++ b/callbacks/delete.go @@ -60,7 +60,7 @@ func Delete(db *gorm.DB) { db.Statement.Build("DELETE", "FROM", "WHERE") } - if !db.DryRun { + if !db.DryRun && db.Error == nil { result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...) if err == nil { diff --git a/callbacks/query.go b/callbacks/query.go index 5c322a05..66bbf805 100644 --- a/callbacks/query.go +++ b/callbacks/query.go @@ -23,7 +23,7 @@ func Query(db *gorm.DB) { BuildQuerySQL(db) } - if !db.DryRun { + if !db.DryRun && db.Error == nil { rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...) if err != nil { db.AddError(err) diff --git a/callbacks/update.go b/callbacks/update.go index d549f97b..e492cfc9 100644 --- a/callbacks/update.go +++ b/callbacks/update.go @@ -74,7 +74,7 @@ func Update(db *gorm.DB) { return } - if !db.DryRun { + if !db.DryRun && db.Error == nil { result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...) if err == nil { diff --git a/errors.go b/errors.go index e1b58835..12e64611 100644 --- a/errors.go +++ b/errors.go @@ -7,20 +7,14 @@ import ( var ( // ErrRecordNotFound record not found error ErrRecordNotFound = errors.New("record not found") - // ErrInvalidSQL invalid SQL error, happens when you passed invalid SQL - ErrInvalidSQL = errors.New("invalid SQL") // ErrInvalidTransaction invalid transaction when you are trying to `Commit` or `Rollback` ErrInvalidTransaction = errors.New("no valid transaction") - // ErrUnaddressable unaddressable value - ErrUnaddressable = errors.New("using unaddressable value") // ErrNotImplemented not implemented ErrNotImplemented = errors.New("not implemented") // ErrMissingWhereClause missing where clause ErrMissingWhereClause = errors.New("WHERE conditions required") // ErrUnsupportedRelation unsupported relations ErrUnsupportedRelation = errors.New("unsupported relations") - // ErrPtrStructSupported only ptr of struct supported - ErrPtrStructSupported = errors.New("only ptr of struct supported") // ErrorPrimaryKeyRequired primary keys required ErrorPrimaryKeyRequired = errors.New("primary key required") // ErrorModelValueRequired model value required diff --git a/statement.go b/statement.go index 5f4238ef..310484d8 100644 --- a/statement.go +++ b/statement.go @@ -95,7 +95,9 @@ func (stmt *Statement) QuoteTo(writer clause.Writer, field interface{}) { } if v.Name == clause.PrimaryKey { - if stmt.Schema != nil && stmt.Schema.PrioritizedPrimaryField != nil { + if stmt.Schema == nil { + stmt.DB.AddError(ErrorModelValueRequired) + } else if stmt.Schema.PrioritizedPrimaryField != nil { stmt.DB.Dialector.QuoteTo(writer, stmt.Schema.PrioritizedPrimaryField.DBName) } else if len(stmt.Schema.DBNames) > 0 { stmt.DB.Dialector.QuoteTo(writer, stmt.Schema.DBNames[0])