{"record":{"id":"d3af440e9af6d2f4","repo":"flipped-aurora/gin-vue-admin","slug":"error-d3af44","errorCode":null,"errorMessage":"父菜单已被其他角色的首页占用，请先释放父菜单的首页权限","messagePattern":"父菜单已被其他角色的首页占用，请先释放父菜单的首页权限","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_menu.go","lineNumber":170,"sourceCode":"\t\t\t}\n\n\t\t\t// 检查父菜单下现有子菜单数量\n\t\t\tvar existingChildrenCount int64\n\t\t\terr := tx.Model(&system.SysBaseMenu{}).Where(\"parent_id = ?\", menu.ParentId).Count(&existingChildrenCount).Error\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\t// 如果父菜单原本是叶子菜单（没有子菜单），现在要变成枝干菜单，需要清空其权限分配\n\t\t\tif existingChildrenCount == 0 {\n\t\t\t\t// 检查父菜单是否被其他角色设置为首页\n\t\t\t\tvar defaultRouterCount int64\n\t\t\t\terr := tx.Model(&system.SysAuthority{}).Where(\"default_router = ?\", parentMenu.Name).Count(&defaultRouterCount).Error\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\tif defaultRouterCount > 0 {\n\t\t\t\t\treturn errors.New(\"父菜单已被其他角色的首页占用，请先释放父菜单的首页权限\")\n\t\t\t\t}\n\n\t\t\t\t// 清空父菜单的所有权限分配\n\t\t\t\terr = tx.Where(\"sys_base_menu_id = ?\", menu.ParentId).Delete(&system.SysAuthorityMenu{}).Error\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 创建菜单\n\t\treturn tx.Create(&menu).Error\n\t})\n}\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@function: getBaseMenuTreeMap\n//@description: 获取路由总树map","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_menu.go#L152-L188","documentation":"In AddBaseMenu, when adding a child under a parent menu, the service checks whether any role (SysAuthority) already uses the parent menu's name as its default_router (homepage). If so, reparenting/adding under it would conflict with role homepages, so the transaction aborts with this error.","triggerScenarios":"Adding a menu whose ParentId points to a parent menu whose Name equals some role's default_router value in sys_authority.","commonSituations":"An admin set a parent menu as a role's homepage (default_router), then tries to restructure the tree by hanging new children under it; environment copies where default_router still references an old menu name.","solutions":["Find the blocking role: SELECT authority_id FROM sys_authority WHERE default_router = '<parentName>'.","Change or clear that role's default_router (role homepage setting) and retry the add.","Choose a different parent menu that is not used as any role's homepage."],"exampleFix":"// before\nawait addMenu({ name: 'subPage', parentId: parentMenu.ID }) // parent is some role's default_router\n// after\nawait updateAuthority({ authorityId, defaultRouter: '' }) // release homepage first\nawait addMenu({ name: 'subPage', parentId: parentMenu.ID })","handlingStrategy":"validation","validationCode":"// ask backend or check role settings before adding a child under parentMenu\nconst roles = (await authorityApi.getAuthorityList()).data.list\nconst conflict = roles.some(r => r.defaultRouter === parentMenu.name)\nif (conflict) {\n  alert('父菜单正被用作角色首页，请先在角色设置中释放 default_router')\n  return\n}","typeGuard":null,"tryCatchPattern":"try {\n  await menuApi.addMenu(newMenu)\n} catch (e) {\n  if (String(e.msg).includes('父菜单已被其他角色的首页占用')) {\n    showReleaseHomepageDialog(parentMenu) // guide admin to clear default_router\n  } else throw e\n}","preventionTips":["Track which menus are referenced by sys_authority.default_router before restructuring the tree.","Clear a role's homepage setting before moving/removing its menu.","Audit default_router values after environment copies for stale names."],"tags":["go","gin-vue-admin","menu","authority","business-rule"],"backgroundTag":"default-router-conflict","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}