{"record":{"id":"964fdb6055a98d6b","repo":"flipped-aurora/gin-vue-admin","slug":"sys-security-configs","errorCode":null,"errorMessage":"sys_security_configs默认配置初始化失败!","messagePattern":"sys_security_configs默认配置初始化失败!","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/source/system/security_config.go","lineNumber":49,"sourceCode":"}\n\nfunc (i *initSecurityConfig) TableCreated(ctx context.Context) bool {\n\tdb, ok := ctx.Value(\"db\").(*gorm.DB)\n\tif !ok {\n\t\treturn false\n\t}\n\treturn db.Migrator().HasTable(&sysModel.SysSecurityConfig{})\n}\n\nfunc (i *initSecurityConfig) InitializeData(ctx context.Context) (context.Context, error) {\n\tdb, ok := ctx.Value(\"db\").(*gorm.DB)\n\tif !ok {\n\t\treturn ctx, system.ErrMissingDBContext\n\t}\n\tcfg := sysModel.DefaultSecurityConfig()\n\tcfg.ID = 1\n\tif err := db.Create(&cfg).Error; err != nil {\n\t\treturn ctx, errors.Wrap(err, sysModel.SysSecurityConfig{}.TableName()+\"默认配置初始化失败!\")\n\t}\n\tnext := context.WithValue(ctx, i.InitializerName(), cfg)\n\treturn next, nil\n}\n\nfunc (i *initSecurityConfig) DataInserted(ctx context.Context) bool {\n\tdb, ok := ctx.Value(\"db\").(*gorm.DB)\n\tif !ok {\n\t\treturn false\n\t}\n\tif errors.Is(db.Where(\"id = ?\", 1).First(&sysModel.SysSecurityConfig{}).Error, gorm.ErrRecordNotFound) {\n\t\treturn false\n\t}\n\treturn true\n}\n","sourceCodeStart":31,"sourceCodeEnd":65,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/source/system/security_config.go#L31-L65","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the wrapped GORM error — a duplicate-key on primary ID 1 means the config already exists and you can skip init","AutoMigrate SysSecurityConfig before initialization","Guard with DataInserted(ctx) / FirstOrCreate so an existing default row short-circuits instead of failing","If the config is corrupt, delete the sys_security_configs row with id=1 and re-run init","Verify DB credentials and that the account has INSERT privileges"],"exampleFix":"// before: unconditional Create blows up when ID=1 exists\nif err := db.Create(&cfg).Error; err != nil { ... }\n// after: skip when default config already present\nvar count int64\n_ = db.Model(&sysModel.SysSecurityConfig{}).Count(&count).Error\nif count == 0 {\n    if err := db.Create(&cfg).Error; err != nil { return err }\n}","handlingStrategy":"validation","validationCode":"if err := db.AutoMigrate(&sysModel.SysSecurityConfig{}); err != nil { return err }\nvar count int64\nif err := db.Model(&sysModel.SysSecurityConfig{}).Count(&count).Error; err != nil { return err }\nif count > 0 { return nil } // default config already exists (ID=1)","typeGuard":"func hasDB(ctx context.Context) (*gorm.DB, bool) {\n    db, ok := ctx.Value(\"db\").(*gorm.DB)\n    return db, ok && db != nil\n}","tryCatchPattern":"ctx, err = initSecurityConfig{}.InitializeData(ctx)\nif err != nil {\n    log.Printf(\"security config seed failed: %v (cause: %v)\", err, errors.Cause(err))\n    return err\n}","preventionTips":["Skip seeding when a default row already exists (FirstOrCreate / count check)","AutoMigrate the security config model before init","Delete a corrupt id=1 row explicitly rather than force re-inserting","Ensure the DB user has INSERT privileges on all seed tables"],"tags":["gorm","database","security-config","seed-data","initialization"],"backgroundTag":"seed-data-insert-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}