Add primary key check before delete

This commit is contained in:
Dan Hardman 2016-08-12 11:23:10 +01:00
parent dda88dd08c
commit 546069e74c
2 changed files with 6 additions and 0 deletions

View File

@ -20,6 +20,10 @@ func beforeDeleteCallback(scope *Scope) {
// deleteCallback used to delete data from database or set deleted_at to current time (when using with soft delete)
func deleteCallback(scope *Scope) {
if scope.PrimaryKeyZero() {
scope.db.AddError(ErrNonSpecificDelete)
}
if !scope.HasError() {
var extraOption string
if str, ok := scope.Get("gorm:delete_option"); ok {

View File

@ -6,6 +6,8 @@ import (
)
var (
// ErrNonSpecificDelete no primary key specified for delete, happens when you try to `Delete` a record without a primary key set
ErrNonSpecificDelete = errors.New("no primary key specified for delete")
// ErrRecordNotFound record not found error, happens when haven't find any matched data when looking up with a struct
ErrRecordNotFound = errors.New("record not found")
// ErrInvalidSQL invalid SQL error, happens when you passed invalid SQL