{"record":{"id":"c1687835c399358a","repo":"flipped-aurora/gin-vue-admin","slug":"error-c16878","errorCode":null,"errorMessage":"此角色有用户正在使用禁止删除","messagePattern":"此角色有用户正在使用禁止删除","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"server/service/system/sys_authority.go","lineNumber":139,"sourceCode":"\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 {\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}","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_authority.go#L121-L157","documentation":"DeleteAuthority refuses to delete a role that still has users attached. After loading the role it checks len(auth.Users) from the preloaded Users relation; if any sys_users rows reference this authority via the join table, it returns \"此角色有用户正在使用禁止删除\". This is a referential-integrity guard preventing orphaned users.","triggerScenarios":"DELETE /authority/deleteAuthority where the user_authority join table still contains rows for the role (Preload(\"Users\") yields a non-empty slice).","commonSituations":"Decommissioning a role before offboarding its members; cleanup scripts that delete roles without reassigning users; imported datasets where join rows were preserved but roles were expected to be empty.","solutions":["Reassign the affected users to another role (update their role(s) in user management), then delete the role.","List the users of the role first (role page shows bound users) and remove them from the role one by one.","If the binding is stale, delete the orphan rows from the user_authority join table, then retry the deletion."],"exampleFix":"// before\ndeleteAuthority({ authorityId: 888 }) // users still bound\n\n// after: reassign users first\nawait setUserAuthorities({ userId: 5, authorityIds: [9528] })\nawait setUserAuthorities({ userId: 6, authorityIds: [9528] })\nawait deleteAuthority({ authorityId: 888 })","handlingStrategy":"validation","validationCode":"const bound = (await getAuthorityList()).data.list.find(r => r.authorityId === id)?.users?.length\nif (bound > 0) throw new Error('角色下仍有 ' + bound + ' 个用户，请先转移再删除')","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":["Adopt an offboarding order: reassign users → delete role.","Show bound-user counts on the role list so blockers are visible before delete.","In cleanup scripts, reassign or clear user_authority rows before deleting roles.","Guard deletes in the UI: disable the delete button when userCount > 0."],"tags":["role","referential-integrity","users","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"}