flipped-aurora/gin-vue-admin · warning
数据库未初始化
Error message
数据库未初始化
What it means
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.
Source
Thrown at server/service/system/sys_security_config.go:38
func setSecurityConfigCache(cfg system.SysSecurityConfig) {
securityConfigCache.Store(cfg)
}
func getSecurityConfigCache() system.SysSecurityConfig {
if v := securityConfigCache.Load(); v != nil {
return v.(system.SysSecurityConfig)
}
return system.SysSecurityConfig{}
}
// Get 读取单行配置 不存在则按代码默认值创建并返回
func (s *SecurityConfigService) Get(ctx context.Context) (system.SysSecurityConfig, error) {
var cfg system.SysSecurityConfig
// 系统尚未初始化(未走 init 向导)或连库失败时 global.GVA_DB 为 nil
// 此时返回代码默认配置并带错误: 调用方 Current 据此不写缓存
// 待数据库就绪后再惰性加载真实行 同时避免对 nil 的 *gorm.DB 解引用导致 panic
if global.GVA_DB == nil {
return system.DefaultSecurityConfig(), errors.New("数据库未初始化")
}
err := global.GVA_DB.WithContext(ctx).Where("id = ?", 1).First(&cfg).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
cfg = system.DefaultSecurityConfig()
cfg.ID = 1
if err = global.GVA_DB.WithContext(ctx).Create(&cfg).Error; err != nil {
return cfg, err
}
return cfg, nil
}
return cfg, err
}
// Set 持久化配置 刷新内存缓存 密码过期由关变开时回填存量 NULL 用户
func (s *SecurityConfigService) Set(ctx context.Context, cfg system.SysSecurityConfig) error {
prev, err := s.Get(ctx)
if err != nil {
return errView on GitHub (pinned to 3136500ef3)
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.
Example fix
// before: calling Current against a fresh, uninitialized backend -> error
// after: ensure DB init first (init wizard), or guard in caller
cfg, err := securityConfigService.Current(c.Request.Context())
if err != nil && err.Error() == "数据库未初始化" {
// use returned default config; skip cache write
} Defensive patterns
Strategy: fallback
Validate before calling
if global.GVA_DB == nil {
// use system.DefaultSecurityConfig() and skip persistence
} Try / catch
cfg, err := svc.Get(ctx)
if err != nil {
// err indicates DB not ready; cfg holds defaults
log.Printf("security config fallback to defaults: %v", err)
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- media.FileUploadAndDownload表数据初始化失败!
- sys_apis表数据初始化失败!
- sys_ignore_apis表数据初始化失败!
- %s表数据初始化失败!
- sys_positions表数据初始化失败!
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/6c38a91292da9fa8.
Report an issue: GitHub.