{"record":{"id":"cb51c62e0ad8d4bc","repo":"flipped-aurora/gin-vue-admin","slug":"error-cb51c6","errorCode":null,"errorMessage":"此角色存在子角色不允许删除","messagePattern":"此角色存在子角色不允许删除","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"server/service/system/sys_authority.go","lineNumber":145,"sourceCode":"\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 {\n\t\t\tif err = tx.Model(auth).Association(\"SysBaseMenus\").Delete(auth.SysBaseMenus); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\t// err = db.Association(\"SysBaseMenus\").Delete(&auth)\n\t\t}\n\n\t\tif err = tx.Delete(&system.SysUserAuthority{}, \"sys_authority_authority_id = ?\", auth.AuthorityId).Error; err != nil {\n\t\t\treturn err\n\t\t}","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_authority.go#L127-L163","documentation":"DeleteAuthority blocks deletion of any role that still has child roles. It queries sys_authorities for a row with parent_id equal to the target's authority_id; if one exists, it returns \"此角色存在子角色不允许删除\". This preserves the role tree's integrity — parents cannot be removed while children point at them.","triggerScenarios":"DELETE /authority/deleteAuthority on a parent role that still has at least one child (parent_id referencing it in sys_authorities).","commonSituations":"Flattening a role hierarchy from the leaf up but missing a hidden/duplicated child; data imported with parent_id values pointing at roles assumed deleted; attempts to delete an intermediate role like 888 when 8881 is still its child.","solutions":["Delete the child roles first (leaf-to-root order), then delete the parent.","Re-parent the children to another role (update their parent_id), then delete the original parent.","Locate the blockers with: SELECT authority_id, authority_name FROM sys_authorities WHERE parent_id = <role>;"],"exampleFix":"// before\ndeleteAuthority({ authorityId: 888 }) // child 8881 exists\n\n// after\ndeleteAuthority({ authorityId: 8881 }) // child first\ndeleteAuthority({ authorityId: 888 })  // then parent","handlingStrategy":"validation","validationCode":"const children = (await getAuthorityList()).data.list.filter(r => r.parentId === id)\nif (children.length > 0) throw new Error('存在子角色：' + children.map(c => c.authorityId).join(', ') + '，请先删除子角色')","typeGuard":null,"tryCatchPattern":"try {\n  await deleteAuthority({ authorityId: id })\n} catch (e) {\n  if (String(e?.msg).includes('此角色存在子角色不允许删除')) {\n    ElMessage.warning('请先删除或转移该角色的子角色')\n  } else { throw e }\n}","preventionTips":["Delete role trees leaf-first (children before parents).","Before deleting, query children: SELECT authority_id FROM sys_authorities WHERE parent_id = <role>.","When flattening a hierarchy, explicitly re-parent or remove every child.","After imports, validate parent_id values all point at roles you intend to keep."],"tags":["role","hierarchy","referential-integrity","validation"],"backgroundTag":"record-in-use-cannot-delete","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}