flipped-aurora/gin-vue-admin · critical

db no init

Error message

db no init

What it means

MustGetGlobalDBByDBName looks up a named *gorm.DB in the global GVA_DBList registry and panics with the literal 'db no init' when the name is absent or the stored handle is nil. The Must* prefix signals the caller contract: the DB must already be registered during initialization, so absence is a programming/configuration error worth crashing on.

Source

Thrown at server/global/global.go:57

	GVA_MCP_SERVER          *server.MCPServer
	GVA_CACHE               gva_cache.Cache
	lock                    sync.RWMutex
)

// GetGlobalDBByDBName 通过名称获取db list中的db
func GetGlobalDBByDBName(dbname string) *gorm.DB {
	lock.RLock()
	defer lock.RUnlock()
	return GVA_DBList[dbname]
}

// MustGetGlobalDBByDBName 通过名称获取db 如果不存在则panic
func MustGetGlobalDBByDBName(dbname string) *gorm.DB {
	lock.RLock()
	defer lock.RUnlock()
	db, ok := GVA_DBList[dbname]
	if !ok || db == nil {
		panic("db no init")
	}
	return db
}

func GetRedis(name string) redis.UniversalClient {
	redis, ok := GVA_REDISList[name]
	if !ok || redis == nil {
		panic(fmt.Sprintf("redis `%s` no init", name))
	}
	return redis
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Add the db name to the config datasource / db-list and ensure initialize.Gorm runs at startup
  2. Fix the dbname string passed to MustGetGlobalDBByDBName to match a registered key exactly
  3. Check startup logs for an earlier gorm connection failure that prevented registration
  4. In tests, initialize GVA_DBList with the needed entries (e.g. server/internal/testutil.NewMemoryDB) before calling code that fetches by name

Example fix

// before
db := global.MustGetGlobalDBByDBName("biz") // not in config
// after (config.yaml)
system:
  db-list: ["pgsql", "biz"]
// or guard:
if db := global.GetGlobalDBByDBName("biz"); db != nil { ... }
Defensive patterns

Strategy: type-guard

Validate before calling

func dbReady(name string) bool { return global.GetGlobalDBByDBName(name) != nil } // call before Must variant

Type guard

func safeDB(name string) *gorm.DB { lock.RLock(); defer lock.RUnlock(); if db, ok := global.GVA_DBList[name]; ok && db != nil { return db }; return nil }

Try / catch

// recover around initialization-dependent calls
func getDBSafe(name string) (db *gorm.DB, err error) { defer func() { if r := recover(); r != nil { if s, ok := r.(string); ok && s == "db no init" { err = fmt.Errorf("database %q not initialized", name); return }; panic(r) } }(); return global.MustGetGlobalDBByDBName(name), nil }

Prevention

When it happens

Trigger: Calling MustGetGlobalDBByDBName("name") before initialize.Gorm/DBList has populated GVA_DBList, using a db-name that is not present in config.system.db-list / the datasource map, or after a failed DB connection left the entry nil.

Common situations: Typo in the business db name vs config.yaml datasource entries; calling a service that uses a secondary DB before initialization in a test without testutil.NewMemoryDB setup; connection failure during bootstrap silently skipping registration.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/10b082395a98865f. Report an issue: GitHub.