gogo-gorm95% confidence\u2191 6

How to handle GORM error at Delete function?

Full error message
I have this function:

var db *gorm.DB

func DeleteCategory(id uint) error {
    var category Category
    category.ID = id
    result := db.Delete(&category)
    fmt.Println("result.Error: ", result.Error)
    return result.Error
}

This function should delete row in the database and it is, but when I call this function multiple time with the same id, I expect it to throw error message at the second call, but it always return nil:
result.Error:  <nil>
*I'm using postgreSQL for the database

Trying to delete a row that doesn't exist is not considered an error by the SQL standard. If you need to return an error you should check the RowsAffected field. func DeleteCategory(id uint) error { c := Category{ID:id} db := db.Delete(&c) if db.Error != nil { return db.Error } else if db.RowsAffected < 1 { return fmt.Errorf("row with id=%d cannot be deleted because it doesn't exist", id) } return nil }

API access

Get this solution programmatically \u2014 free, no authentication.

curl https://depscope.dev/api/error/9945ebc36e3ac9c5db34a7e5d849d43dc2ddbcff44daa5d60e754b8a258fbc8d
hash \u00b7 9945ebc36e3ac9c5db34a7e5d849d43dc2ddbcff44daa5d60e754b8a258fbc8d
How to handle GORM error at Delete function? — DepScope fix | DepScope