flipped-aurora/gin-vue-admin · error

sys_security_configs默认配置初始化失败!

Error message

sys_security_configs默认配置初始化失败!

What it means

Thrown by the security-config initializer (initSecurityConfig.InitializeData) when inserting the single default row from sysModel.DefaultSecurityConfig() with forced ID=1 into sys_security_configs fails. It wraps the GORM error and names the table. This row backs the login security settings (captcha thresholds, lockout) so init cannot proceed without it.

Source

Thrown at server/source/system/security_config.go:49

}

func (i *initSecurityConfig) TableCreated(ctx context.Context) bool {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return false
	}
	return db.Migrator().HasTable(&sysModel.SysSecurityConfig{})
}

func (i *initSecurityConfig) InitializeData(ctx context.Context) (context.Context, error) {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return ctx, system.ErrMissingDBContext
	}
	cfg := sysModel.DefaultSecurityConfig()
	cfg.ID = 1
	if err := db.Create(&cfg).Error; err != nil {
		return ctx, errors.Wrap(err, sysModel.SysSecurityConfig{}.TableName()+"默认配置初始化失败!")
	}
	next := context.WithValue(ctx, i.InitializerName(), cfg)
	return next, nil
}

func (i *initSecurityConfig) DataInserted(ctx context.Context) bool {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return false
	}
	if errors.Is(db.Where("id = ?", 1).First(&sysModel.SysSecurityConfig{}).Error, gorm.ErrRecordNotFound) {
		return false
	}
	return true
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Inspect the wrapped GORM error — a duplicate-key on primary ID 1 means the config already exists and you can skip init
  2. AutoMigrate SysSecurityConfig before initialization
  3. Guard with DataInserted(ctx) / FirstOrCreate so an existing default row short-circuits instead of failing
  4. If the config is corrupt, delete the sys_security_configs row with id=1 and re-run init
  5. Verify DB credentials and that the account has INSERT privileges

Example fix

// before: unconditional Create blows up when ID=1 exists
if err := db.Create(&cfg).Error; err != nil { ... }
// after: skip when default config already present
var count int64
_ = db.Model(&sysModel.SysSecurityConfig{}).Count(&count).Error
if count == 0 {
    if err := db.Create(&cfg).Error; err != nil { return err }
}
Defensive patterns

Strategy: validation

Validate before calling

if err := db.AutoMigrate(&sysModel.SysSecurityConfig{}); err != nil { return err }
var count int64
if err := db.Model(&sysModel.SysSecurityConfig{}).Count(&count).Error; err != nil { return err }
if count > 0 { return nil } // default config already exists (ID=1)

Type guard

func hasDB(ctx context.Context) (*gorm.DB, bool) {
    db, ok := ctx.Value("db").(*gorm.DB)
    return db, ok && db != nil
}

Try / catch

ctx, err = initSecurityConfig{}.InitializeData(ctx)
if err != nil {
    log.Printf("security config seed failed: %v (cause: %v)", err, errors.Cause(err))
    return err
}

Prevention

When it happens

Trigger: db.Create(&cfg) fails: sys_security_configs not migrated, a row with ID=1 already exists (primary-key conflict, e.g. from a previous init or manual edit), NOT NULL/default mismatch after schema change, or DB connectivity failure.

Common situations: Re-running init on a database that already has the default config row (duplicate primary key 1); upgrading where the security config table gained columns and old rows/seed conflict; sqlite file locked by another process during tests; DB user lacking INSERT privilege.

Related errors


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