flipped-aurora/gin-vue-admin · critical
自动代码数据库未初始化
Error message
自动代码数据库未初始化
What it means
persistAutoCodeMetadata requires a non-nil *gorm.DB handle; if db == nil it refuses to run and returns this error before opening a transaction. It protects the multi-row insert (APIs, menu, history) from nil-pointer panics when the caller has no initialized database connection.
Source
Thrown at server/service/system/auto_code_persistence.go:24
"errors"
"fmt"
model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
"gorm.io/gorm"
)
var errAutoCodeMenuConflict = errors.New("自动代码菜单名称冲突")
func persistAutoCodeMetadata(
ctx context.Context,
db *gorm.DB,
info request.AutoCode,
packageTemplate string,
history request.SysAutoHistoryCreate,
) error {
if db == nil {
return errors.New("自动代码数据库未初始化")
}
return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := persistAutoCodeAPIs(tx, info, &history); err != nil {
return err
}
if err := persistAutoCodeMenu(tx, info, packageTemplate, &history); err != nil {
return err
}
if err := persistAutoCodeExportTemplate(tx, info, &history); err != nil {
return err
}
entity := history.Create()
if err := tx.Create(&entity).Error; err != nil {
return fmt.Errorf("创建自动代码历史失败: %w", err)
}
return nil
})
}View on GitHub (pinned to 3136500ef3)
Solutions
- Initialize the DB before invoking persistence: ensure config.yaml database settings are correct so global.GVA_DB is set at startup.
- In tests, use server/internal/testutil.NewMemoryDB(t, models...) to inject an in-memory GORM DB.
- Add a startup assertion that global.GVA_DB != nil to fail fast on config errors.
Example fix
// before
db := global.GVA_DB // nil when uninitialized
err := persistAutoCodeMetadata(ctx, db, info, template, history)
// after
if global.GVA_DB == nil {
return errors.New("数据库未初始化,请检查 config.yaml 数据库配置")
}
err := persistAutoCodeMetadata(ctx, global.GVA_DB, info, template, history) Defensive patterns
Strategy: validation
Validate before calling
if db == nil {
return errors.New("自动代码数据库未初始化")
}
// or at caller:
if global.GVA_DB == nil {
return errors.New("数据库连接未初始化,请检查启动配置")
} Type guard
func dbReady(db *gorm.DB) bool {
return db != nil
} Try / catch
if err := persistAutoCodeMetadata(ctx, db, info, template, history); err != nil {
if err.Error() == "自动代码数据库未初始化" {
return fmt.Errorf("数据库未连接: %w", err)
}
return err
} Prevention
- Verify startup logs show successful DB connection before using auto-code.
- In tests always construct the DB via server/internal/testutil.NewMemoryDB.
- Fail fast at boot if global.GVA_DB is nil.
When it happens
Trigger: Calling persistAutoCodeMetadata (directly or via the service entry) with a nil db argument — typically when global.GVA_DB was never initialized or a caller passed nil explicitly.
Common situations: Unit/integration tests constructing the service without testutil.NewMemoryDB; misconfigured database so initialization silently skipped; calling persistence logic before server bootstrap completed.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/77bd64c54631f6a2.
Report an issue: GitHub.