{"record":{"id":"00173bf6b9620a83","repo":"flipped-aurora/gin-vue-admin","slug":"error-00173b","errorCode":null,"errorMessage":"为普通用户分配菜单失败","messagePattern":"为普通用户分配菜单失败","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/source/system/authorities_menus.go","lineNumber":75,"sourceCode":"\t// 为不同角色分配不同权限\n\t// 1. 超级管理员角色(888) - 拥有所有菜单权限\n\tif err = db.Model(&authorities[0]).Association(\"SysBaseMenus\").Replace(allMenus); err != nil {\n\t\treturn next, errors.Wrap(err, \"为超级管理员分配菜单失败\")\n\t}\n\n\t// 2. 普通用户角色(8881) - 仅拥有基础功能菜单\n\t// 仅选择部分父级菜单及其子菜单\n\tvar menu8881 []sysModel.SysBaseMenu\n\n\t// 添加仪表盘、关于我们和个人信息菜单\n\tfor _, menu := range allMenus {\n\t\tif menu.ParentId == 0 && (menu.Name == \"dashboard\" || menu.Name == \"about\" || menu.Name == \"person\" || menu.Name == \"state\") {\n\t\t\tmenu8881 = append(menu8881, menu)\n\t\t}\n\t}\n\n\tif err = db.Model(&authorities[1]).Association(\"SysBaseMenus\").Replace(menu8881); err != nil {\n\t\treturn next, errors.Wrap(err, \"为普通用户分配菜单失败\")\n\t}\n\n\t// 3. 测试角色(9528) - 拥有部分菜单权限\n\tvar menu9528 []sysModel.SysBaseMenu\n\n\t// 添加所有父级菜单\n\tfor _, menu := range allMenus {\n\t\tif menu.ParentId == 0 {\n\t\t\tmenu9528 = append(menu9528, menu)\n\t\t}\n\t}\n\n\t// 添加部分子菜单 - 系统工具、示例文件等模块的子菜单\n\tfor _, menu := range allMenus {\n\t\tparentName := \"\"\n\t\tif menu.ParentId > 0 && menuMap[menu.ParentId].Name != \"\" {\n\t\t\tparentName = menuMap[menu.ParentId].Name\n\t\t}","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/source/system/authorities_menus.go#L57-L93","documentation":"initMenuAuthority assigns the basic menu set to the normal-user role (authorities[1], ID 8881) via Association Replace on SysBaseMenus. The GORM operation failed and was wrapped with this message, leaving role 8881 without menu bindings.","triggerScenarios":"Replace on the sys_authorities_menus join table fails for authority 8881 - missing/stale join table schema, FK violation because seeded menus or the authority row are absent, connection failure, or write privilege denied.","commonSituations":"Re-init on a DB where sys_authorities were seeded differently (fewer than 3 roles, so authorities[1] is the wrong row); interrupted previous init leaving partial join rows; older schema after upgrading gin-vue-admin.","solutions":["Inspect the wrapped cause (unknown column, FK failure, access denied).","Re-run a clean InitDB on an empty database so authority/menu seeds and join rows stay consistent.","Confirm AutoMigrate created sys_authorities_menus with the current version's schema.","Ensure initAuthority produced all 4 seeded roles so authorities[1] is the expected 8881 role.","Verify DB write permissions and retry init."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// guard index and table existence before Replace\nif len(authorities) < 2 { return errors.New(\"authority seed incomplete\") }\nif !db.Migrator().HasTable(\"sys_authorities_menus\") { return errors.New(\"join table missing\") }","typeGuard":null,"tryCatchPattern":"if err := db.Model(&authorities[1]).Association(\"SysBaseMenus\").Replace(menu8881); err != nil {\n    return fmt.Errorf(\"role 8881 menu binding failed: %w\", err)\n}","preventionTips":["Verify initAuthority seeded all expected roles before this step","Avoid re-init over partially written join rows - re-init from empty DB","Keep AutoMigrate enabled so join-table schema matches","Check driver error via errors.Unwrap for FK/privilege issues"],"tags":["database","gorm","association","seed-data"],"backgroundTag":"db-seed-insert-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}