{"record":{"id":"8103390febcfdd4b","repo":"flipped-aurora/gin-vue-admin","slug":"error-810339","errorCode":null,"errorMessage":"该角色不存在","messagePattern":"该角色不存在","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_authority.go","lineNumber":136,"sourceCode":"\tvar oldAuthority system.SysAuthority\n\terr = global.GVA_DB.WithContext(ctx).Where(\"authority_id = ?\", auth.AuthorityId).First(&oldAuthority).Error\n\tif err != nil {\n\t\tlogger.WithCtx(ctx).Mod(\"biz\").Debug(err.Error())\n\t\treturn system.SysAuthority{}, errors.New(\"查询角色数据失败\")\n\t}\n\terr = global.GVA_DB.WithContext(ctx).Model(&oldAuthority).Updates(&auth).Error\n\treturn auth, err\n}\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@function: DeleteAuthority\n//@description: 删除角色\n//@param: auth *model.SysAuthority\n//@return: err error\n\nfunc (authorityService *AuthorityService) DeleteAuthority(ctx context.Context, auth *system.SysAuthority) error {\n\tif errors.Is(global.GVA_DB.WithContext(ctx).Preload(\"Users\").First(&auth).Error, gorm.ErrRecordNotFound) {\n\t\treturn errors.New(\"该角色不存在\")\n\t}\n\tif len(auth.Users) != 0 {\n\t\treturn errors.New(\"此角色有用户正在使用禁止删除\")\n\t}\n\tif !errors.Is(global.GVA_DB.WithContext(ctx).Where(\"authority_id = ?\", auth.AuthorityId).First(&system.SysUser{}).Error, gorm.ErrRecordNotFound) {\n\t\treturn errors.New(\"此角色有用户正在使用禁止删除\")\n\t}\n\tif !errors.Is(global.GVA_DB.WithContext(ctx).Where(\"parent_id = ?\", auth.AuthorityId).First(&system.SysAuthority{}).Error, gorm.ErrRecordNotFound) {\n\t\treturn errors.New(\"此角色存在子角色不允许删除\")\n\t}\n\n\treturn global.GVA_DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {\n\t\tvar err error\n\t\tif err = tx.Preload(\"SysBaseMenus\").Where(\"authority_id = ?\", auth.AuthorityId).First(auth).Unscoped().Delete(auth).Error; err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tif len(auth.SysBaseMenus) > 0 {","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_authority.go#L118-L154","documentation":"DeleteAuthority loads the role (with preloaded Users) before deleting; if the First() returns gorm.ErrRecordNotFound it returns \"该角色不存在\". The role cannot be deleted because it does not exist in sys_authorities at the time of the call.","triggerScenarios":"DELETE /authority/deleteAuthority with an authorityId absent from sys_authorities — double-delete (same role removed twice), deleting an already-deleted role from a stale page, or deleting a role from the wrong environment/database.","commonSituations":"Concurrent admins deleting the same role; frontend list not refreshed after another deletion; scripts replaying deletion requests; environment mismatch (staging ID used against production DB).","solutions":["Refresh the role list and confirm the role is already gone — no action needed.","Verify the authorityId in the request matches an existing role.","Check you are connected to the intended database/environment."],"exampleFix":"// before: blind delete that can double-fire\nawait Promise.all(ids.map(id => deleteAuthority({ authorityId: id })))\n\n// after: dedupe and tolerate already-deleted\nawait Promise.all([...new Set(ids)].map(async id => {\n  try { await deleteAuthority({ authorityId: id }) } catch (e) {\n    if (!String(e.msg).includes('该角色不存在')) throw e\n  }\n}))","handlingStrategy":"try-catch","validationCode":"const roles = (await getAuthorityList()).data.list\nif (!roles.some(r => r.authorityId === id)) throw new Error('角色已不存在，跳过删除')","typeGuard":null,"tryCatchPattern":"try {\n  await deleteAuthority({ authorityId: id })\n} catch (e) {\n  if (String(e?.msg).includes('该角色不存在')) {\n    ElMessage.info('角色已被删除，无需重复操作')\n    refreshList()\n  } else { throw e }\n}","preventionTips":["Make deletion flows idempotent — treat 'not found' on delete as success.","Refresh the role list immediately after any deletion to avoid double-delete from stale UI.","Deduplicate IDs before batch deletes.","Verify environment/DB targeting when deleting by ID from scripts."],"tags":["role","record-not-found","gorm","concurrency"],"backgroundTag":"record-not-found","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}