I have the following code
func (r *WorkspaceRepository) Delete(id any) (bool, error) {
if err := r.db.Where("id = ?", id).Delete(&model.Workspace{}).Error; err != nil {
return false, err
}
return true, nil
}
When I pass an ID that does not exist, no errors are returned, as if the record existed!
What do I need to do to check before deleting, do I need to do a SELECT first?
Delete method does not return ErrRecordNotFound error. Docs here: https://gorm.io/docs/error_handling.html
GORM returns ErrRecordNotFound when failed to find data with First, Last, Take
Code:
r.db.Where("id = ?", id).Delete(&model.Workspace{})
return gorm.DB struct and you can check if any item deleted or not
tx := r.db.Where("id = ?", id).Delete(&model.Workspace{})
fmt.Println(tx.RowsAffected)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With