Add primary key check before delete
This commit is contained in:
		
							parent
							
								
									dda88dd08c
								
							
						
					
					
						commit
						546069e74c
					
				@ -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)
 | 
					// deleteCallback used to delete data from database or set deleted_at to current time (when using with soft delete)
 | 
				
			||||||
func deleteCallback(scope *Scope) {
 | 
					func deleteCallback(scope *Scope) {
 | 
				
			||||||
 | 
						if scope.PrimaryKeyZero() {
 | 
				
			||||||
 | 
							scope.db.AddError(ErrNonSpecificDelete)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if !scope.HasError() {
 | 
						if !scope.HasError() {
 | 
				
			||||||
		var extraOption string
 | 
							var extraOption string
 | 
				
			||||||
		if str, ok := scope.Get("gorm:delete_option"); ok {
 | 
							if str, ok := scope.Get("gorm:delete_option"); ok {
 | 
				
			||||||
 | 
				
			|||||||
@ -6,6 +6,8 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					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 record not found error, happens when haven't find any matched data when looking up with a struct
 | 
				
			||||||
	ErrRecordNotFound = errors.New("record not found")
 | 
						ErrRecordNotFound = errors.New("record not found")
 | 
				
			||||||
	// ErrInvalidSQL invalid SQL error, happens when you passed invalid SQL
 | 
						// ErrInvalidSQL invalid SQL error, happens when you passed invalid SQL
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user