{"record":{"id":"bc21b5dfaacf8d54","repo":"flipped-aurora/gin-vue-admin","slug":"error-bc21b5","errorCode":null,"errorMessage":"创建 [菜单-权限] 关联失败, 未找到权限表初始化数据","messagePattern":"创建 \\[菜单-权限\\] 关联失败, 未找到权限表初始化数据","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/source/system/authorities_menus.go","lineNumber":42,"sourceCode":"\nfunc (i *initMenuAuthority) TableCreated(ctx context.Context) bool {\n\treturn false // always replace\n}\n\nfunc (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, \"为超级管理员分配菜单失败\")","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/source/system/authorities_menus.go#L24-L60","documentation":"initMenuAuthority builds role-menu associations from data produced by earlier initializers. It reads the initAuthority result from the context and, if it is absent or not a []sysModel.SysAuthority, wraps ErrMissingDependentContext with this message. It means the authority initializer never ran or its result was lost before this step.","triggerScenarios":"Calling InitializeData for initMenuAuthority without the initializer chain having run initAuthority first - e.g. invoking an initializer out of order, a prior initializer aborted so its result was never stored via context.WithValue, or DataInserted checks were bypassed.","commonSituations":"Custom init code that calls initializers individually in the wrong order; a earlier initializer failed silently in a wrapper that swallowed the abort; modifying the init order in registerInitializers; partial init where authorities table creation was skipped.","solutions":["Run the full initializer chain (or /init/initdb) rather than invoking initMenuAuthority alone.","Ensure initAuthority runs before initMenuAuthority and check/propagate its returned error.","Verify the context passed in is the one returned by the previous initializer (chain the next ctx).","Check registerInitializers order matches dependencies: authority before authorities_menus.","If the DB is already initialized, skip re-init; DataInserted should return true and this path not execute."],"exampleFix":"// before: step called out of order\nctx, err := initMenuAuthority.InitializeData(ctx) // missing authority data\n// after: run chain in dependency order\nctx, err = initAuthority.InitializeData(ctx)\nif err != nil {\n    return err\n}\nctx, err = initMenuAuthority.InitializeData(ctx)","handlingStrategy":"validation","validationCode":"// verify the dependent initializer output exists in ctx before calling\nauths, ok := ctx.Value(new(initAuthority).InitializerName()).([]sysModel.SysAuthority)\nif !ok || len(auths) == 0 {\n    return errors.New(\"run initAuthority.InitializeData before menu-authority binding\")\n}","typeGuard":"func hasAuthorityContext(ctx context.Context) bool {\n    auths, ok := ctx.Value(new(initAuthority).InitializerName()).([]sysModel.SysAuthority)\n    return ok && len(auths) > 0\n}","tryCatchPattern":"next, err := initMenuAuthority.InitializeData(ctx)\nif errors.Is(err, system.ErrMissingDependentContext) {\n    return fmt.Errorf(\"initializer chain broken - re-run full initdb: %w\", err)\n}","preventionTips":["Always run the full initializer chain instead of calling initializers individually","Chain the returned context (next) into the following initializer","Keep registerInitializers order: authority before authorities_menus","Check every prior initializer's error before proceeding"],"tags":["initialization","context","dependency-order","seed-data"],"backgroundTag":"missing-dependent-init-context","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}