{"id":1253,"hash":"9945ebc36e3ac9c5db34a7e5d849d43dc2ddbcff44daa5d60e754b8a258fbc8d","pattern":"How to handle GORM error at Delete function?","full_message":"I have this function:\n\nvar db *gorm.DB\n\nfunc DeleteCategory(id uint) error {\n    var category Category\n    category.ID = id\n    result := db.Delete(&category)\n    fmt.Println(\"result.Error: \", result.Error)\n    return result.Error\n}\n\nThis 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:\nresult.Error:  <nil>\n*I'm using postgreSQL for the database","ecosystem":"go","package_name":"go-gorm","package_version":null,"solution":"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.\n\nfunc DeleteCategory(id uint) error {\n    c := Category{ID:id}\n\n    db := db.Delete(&c)\n    if db.Error != nil {\n        return db.Error\n    } else if db.RowsAffected < 1 {\n        return fmt.Errorf(\"row with id=%d cannot be deleted because it doesn't exist\", id)\n    }\n\n    return nil\n}","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/67154864/how-to-handle-gorm-error-at-delete-function","votes":6,"created_at":"2026-04-19T04:52:42.963009+00:00","updated_at":"2026-04-19T04:52:42.963009+00:00"}