{"record":{"id":"6c38a91292da9fa8","repo":"flipped-aurora/gin-vue-admin","slug":"error-6c38a9","errorCode":null,"errorMessage":"数据库未初始化","messagePattern":"数据库未初始化","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"server/service/system/sys_security_config.go","lineNumber":38,"sourceCode":"func setSecurityConfigCache(cfg system.SysSecurityConfig) {\n\tsecurityConfigCache.Store(cfg)\n}\n\nfunc getSecurityConfigCache() system.SysSecurityConfig {\n\tif v := securityConfigCache.Load(); v != nil {\n\t\treturn v.(system.SysSecurityConfig)\n\t}\n\treturn system.SysSecurityConfig{}\n}\n\n// Get 读取单行配置 不存在则按代码默认值创建并返回\nfunc (s *SecurityConfigService) Get(ctx context.Context) (system.SysSecurityConfig, error) {\n\tvar cfg system.SysSecurityConfig\n\t// 系统尚未初始化(未走 init 向导)或连库失败时 global.GVA_DB 为 nil\n\t// 此时返回代码默认配置并带错误: 调用方 Current 据此不写缓存\n\t// 待数据库就绪后再惰性加载真实行 同时避免对 nil 的 *gorm.DB 解引用导致 panic\n\tif global.GVA_DB == nil {\n\t\treturn system.DefaultSecurityConfig(), errors.New(\"数据库未初始化\")\n\t}\n\terr := global.GVA_DB.WithContext(ctx).Where(\"id = ?\", 1).First(&cfg).Error\n\tif errors.Is(err, gorm.ErrRecordNotFound) {\n\t\tcfg = system.DefaultSecurityConfig()\n\t\tcfg.ID = 1\n\t\tif err = global.GVA_DB.WithContext(ctx).Create(&cfg).Error; err != nil {\n\t\t\treturn cfg, err\n\t\t}\n\t\treturn cfg, nil\n\t}\n\treturn cfg, err\n}\n\n// Set 持久化配置 刷新内存缓存 密码过期由关变开时回填存量 NULL 用户\nfunc (s *SecurityConfigService) Set(ctx context.Context, cfg system.SysSecurityConfig) error {\n\tprev, err := s.Get(ctx)\n\tif err != nil {\n\t\treturn err","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_security_config.go#L20-L56","documentation":"SecurityConfigService.Get returns this error when global.GVA_DB is nil, meaning the system has not completed the init wizard or the DB connection failed. Instead of panicking on a nil *gorm.DB, it returns the built-in default security config along with this error so callers like Current skip cache writes. Once the database is available, the real row is lazily loaded.","triggerScenarios":"Calling Get/Current/LoadAll (or Set) before running the system init wizard, or when the backend started with an unreachable/misconfigured database so global.GVA_DB was never assigned.","commonSituations":"Fresh deployments where the admin UI config page is opened before init; Docker/first boot where MySQL is not yet reachable; wrong DSN in config.yaml causing gorm connect failure at startup.","solutions":["Complete the system init wizard (or verify the DB is up) so global.GVA_DB is initialized, then retry.","Check server/config.yaml database DSN/host/credentials and fix connectivity; restart the server.","If hitting this in code, treat the returned config as defaults and do not attempt to persist settings until DB is ready.","In tests, initialize global.GVA_DB (e.g. via server/internal/testutil.NewMemoryDB) before calling the service."],"exampleFix":"// before: calling Current against a fresh, uninitialized backend -> error\n// after: ensure DB init first (init wizard), or guard in caller\ncfg, err := securityConfigService.Current(c.Request.Context())\nif err != nil && err.Error() == \"数据库未初始化\" {\n    // use returned default config; skip cache write\n}","handlingStrategy":"fallback","validationCode":"if global.GVA_DB == nil {\n    // use system.DefaultSecurityConfig() and skip persistence\n}","typeGuard":null,"tryCatchPattern":"cfg, err := svc.Get(ctx)\nif err != nil {\n    // err indicates DB not ready; cfg holds defaults\n    log.Printf(\"security config fallback to defaults: %v\", err)\n}","preventionTips":["Complete the init wizard before exposing config pages.","Add startup health checks that verify DB connectivity before serving traffic.","In tests, always initialize global.GVA_DB via testutil helpers.","Treat the returned default config as read-only until DB is ready."],"tags":["database","initialization","gorm","graceful-degradation"],"backgroundTag":"database-not-initialized","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}