flipped-aurora/gin-vue-admin · critical

db not init

Error message

db not init

What it means

UserService.Login checks global.GVA_DB before querying the user; if the global DB singleton is nil (database never initialized), login is impossible and this sentinel error is returned. It indicates the server booted without a working DB connection rather than a per-request problem.

Source

Thrown at server/service/system/sys_user.go:55

	u.MustChangePassword = cfg.ForceNewUserChangePassword
	u.Password = utils.BcryptHash(u.Password)
	u.UUID = uuid.New()
	now := time.Now()
	u.PasswordUpdatedAt = &now
	err = global.GVA_DB.WithContext(ctx).Create(&u).Error
	return u, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@author: [SliverHorn](https://github.com/SliverHorn)
//@function: Login
//@description: 用户登录
//@param: u *model.SysUser
//@return: err error, userInter *model.SysUser

func (userService *UserService) Login(ctx context.Context, u *system.SysUser) (userInter *system.SysUser, err error) {
	if nil == global.GVA_DB {
		return nil, fmt.Errorf("db not init")
	}

	var user system.SysUser
	err = global.GVA_DB.WithContext(ctx).Where("username = ?", u.Username).Preload("Authorities").Preload("Authority").Preload("Departments").Preload("Positions").Preload("Dept").First(&user).Error
	if err == nil {
		if ok := utils.BcryptCheck(u.Password, user.Password); !ok {
			return nil, errors.New("密码错误")
		}
		MenuServiceApp.UserAuthorityDefaultRouter(ctx, &user)
	}
	return &user, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: ChangePassword
//@description: 修改用户密码
//@param: u *model.SysUser, newPassword string
//@return: err error

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Fix server config.yaml database settings and restart so initialize initializes global.GVA_DB
  2. Check startup logs for DB init errors (connection refused, auth failed) and resolve them
  3. In tests, use the shared testutil helper (testutil.NewMemoryDB(t, ...)) instead of invoking Login on a nil DB

Example fix

// before (test)
user := &system.SysUser{Username:"a"}
_, err := userService.Login(ctx, user) // db not init
// after
db := testutil.NewMemoryDB(t, &system.SysUser{})
_ = db
_, err := userService.Login(ctx, user)
Defensive patterns

Strategy: try-catch

Validate before calling

if global.GVA_DB == nil {
    return errors.New("database not initialized; check config and startup logs")
}

Try / catch

userInter, err := userService.Login(ctx, u)
if err != nil {
    if err.Error() == "db not init" {
        log.Fatal("GVA_DB is nil: verify config.yaml DB settings and initialize sequence")
    }
}

Prevention

When it happens

Trigger: Calling login on a server where GVA initialization failed or was skipped — missing/misnamed config, DB unreachable at startup, running a stripped test binary without init.GlobalDB.

Common situations: Wrong config.yaml (yaml extension vs yml) so viper loads nothing; MySQL down/mis-credentials causing init to skip assignment; unit tests invoking Login without initializing GVA_DB.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


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