{"record":{"id":"4ecb071480e5b38c","repo":"flipped-aurora/gin-vue-admin","slug":"error-4ecb07","errorCode":null,"errorMessage":"创建 [菜单-权限] 关联失败, 未找到菜单表初始化数据","messagePattern":"创建 \\[菜单-权限\\] 关联失败, 未找到菜单表初始化数据","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/source/system/authorities_menus.go","lineNumber":47,"sourceCode":"func (i *initMenuAuthority) InitializerName() string {\n\treturn \"sys_menu_authorities\"\n}\n\nfunc (i *initMenuAuthority) InitializeData(ctx context.Context) (next context.Context, err error) {\n\tdb, ok := ctx.Value(\"db\").(*gorm.DB)\n\tif !ok {\n\t\treturn ctx, system.ErrMissingDBContext\n\t}\n\n\tinitAuth := &initAuthority{}\n\tauthorities, ok := ctx.Value(initAuth.InitializerName()).([]sysModel.SysAuthority)\n\tif !ok {\n\t\treturn ctx, errors.Wrap(system.ErrMissingDependentContext, \"创建 [菜单-权限] 关联失败, 未找到权限表初始化数据\")\n\t}\n\n\tallMenus, ok := ctx.Value(new(initMenu).InitializerName()).([]sysModel.SysBaseMenu)\n\tif !ok {\n\t\treturn next, errors.Wrap(errors.New(\"\"), \"创建 [菜单-权限] 关联失败, 未找到菜单表初始化数据\")\n\t}\n\tnext = ctx\n\n\t// 构建菜单ID映射，方便快速查找\n\tmenuMap := make(map[uint]sysModel.SysBaseMenu)\n\tfor _, menu := range allMenus {\n\t\tmenuMap[menu.ID] = menu\n\t}\n\n\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","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/source/system/authorities_menus.go#L29-L65","documentation":"During system initialization, the authorities_menus initializer reads its prerequisites from the initializer context: the authorities seed data and the menus seed data (keyed by their InitializerName). When the menus value is absent or of the wrong type, it wraps a new error as \"创建 [菜单-权限] 关联失败, 未找到菜单表初始化数据\" via errors.Wrap.","triggerScenarios":"Running the authorities_menus InitializeData (authorities_menus.go:47) when the initMenu initializer did not run first, was disabled in the registration order, or its context value was overwritten/removed by another initializer.","commonSituations":"Custom init order in the initializer registry that skips initMenu; partial initialization after a failed earlier step; developers writing custom initializers that clobber shared context keys; running only selected initializers.","solutions":["Ensure initMenu runs before the authorities_menus initializer in the registration/order list (system/init/*.go).","Check that the menu initializer completed successfully (its data must be in ctx under new(initMenu).InitializerName()).","If you customized initializers, keep the ctx key type and name identical to the built-in initMenu.","Re-run full system initialization rather than a single initializer."],"exampleFix":"// before\n// initOrder: [initDB, initAuthority, initAuthoritiesMenus] // initMenu missing\n// after\n// initOrder: [initDB, initMenu, initAuthority, initAuthoritiesMenus]","handlingStrategy":"validation","validationCode":"// before running authorities_menus init, verify prerequisite in ctx\nif _, ok := ctx.Value(new(initMenu).InitializerName()).([]sysModel.SysBaseMenu); !ok {\n  return errors.New(\"run initMenu initializer first\")\n}","typeGuard":"menus, ok := ctx.Value(new(initMenu).InitializerName()).([]sysModel.SysBaseMenu)\nif !ok { return fmt.Errorf(\"menu init data missing\") }","tryCatchPattern":"if err := initAuthoritiesMenus.InitializeData(ctx); err != nil {\n  if strings.Contains(err.Error(), \"未找到菜单表初始化数据\") {\n    logger.Error(\"initializer order wrong: initMenu must run before authorities_menus\")\n  }\n  return err\n}","preventionTips":["Preserve the built-in initializer registration order (initMenu before authorities_menus).","Do not remove or rename the initMenu context key in custom initializers.","Run full initialization instead of cherry-picking individual initializers."],"tags":["initializer","seed-data","menu","authority","context"],"backgroundTag":"missing-initializer-dependency","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}