{"record":{"id":"1310b71903100748","repo":"flipped-aurora/gin-vue-admin","slug":"sys-export-templates","errorCode":null,"errorMessage":"sys_export_templates表数据初始化失败!","messagePattern":"sys_export_templates表数据初始化失败!","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/source/system/excel_template.go","lineNumber":60,"sourceCode":"\tif !ok {\n\t\treturn ctx, system.ErrMissingDBContext\n\t}\n\n\tentities := []sysModel.SysExportTemplate{\n\t\t{\n\t\t\tName:       \"api\",\n\t\t\tTableName:  \"sys_apis\",\n\t\t\tTemplateID: \"api\",\n\t\t\tTemplateInfo: `{\n\"path\":\"路径\",\n\"method\":\"方法（大写）\",\n\"description\":\"方法介绍\",\n\"api_group\":\"方法分组\"\n}`,\n\t\t},\n\t}\n\tif err := db.Create(&entities).Error; err != nil {\n\t\treturn ctx, errors.Wrap(err, \"sys_export_templates\"+\"表数据初始化失败!\")\n\t}\n\tnext := context.WithValue(ctx, i.InitializerName(), entities)\n\treturn next, nil\n}\n\nfunc (i *initExcelTemplate) 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.First(&sysModel.SysExportTemplate{}).Error, gorm.ErrRecordNotFound) {\n\t\treturn false\n\t}\n\treturn true\n}\n","sourceCodeStart":42,"sourceCodeEnd":76,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/source/system/excel_template.go#L42-L76","documentation":"Thrown by the sys_export_templates initializer (initExcelTemplate.InitializeData) when the bulk insert of the default export-template seed rows fails via db.Create(&entities). The error wraps the underlying GORM error and names sys_export_templates so the failing table is identifiable.","triggerScenarios":"db.Create(&entities) returns an error: table sys_export_templates not migrated, duplicate key from prior partial seeding, NOT NULL/constraint violation in a seed row, or a DB connection failure during the insert.","commonSituations":"Running init against an existing DB where templates were already inserted (unique/duplicate conflict); template JSON column content rejected by strict SQL modes; database read-only or quota exhausted; dialect mismatch after switching mysql→pgsql.","solutions":["Read the wrapped cause for the raw SQL error and address it directly","Ensure db.AutoMigrate(&excelModel.SysExportTemplate{}) runs before initialization","Truncate sys_export_templates and re-run the initializer for a clean seed","Verify DB credentials/connectivity and that the database is writable","Check SQL mode / column constraints against the seed row fields"],"exampleFix":"// before: assumes table exists\nctx, err = initExcelTemplate{}.InitializeData(ctx)\n// after: guard with migration and inserted check\nif err := db.Set(\"gorm:table_options\", \"\").AutoMigrate(&excelModel.SysExportTemplate{}); err != nil { return err }\nif !(initExcelTemplate{}).DataInserted(ctx) {\n    ctx, err = initExcelTemplate{}.InitializeData(ctx)\n}","handlingStrategy":"validation","validationCode":"if !db.Migrator().HasTable(&excelModel.SysExportTemplate{}) {\n    if err := db.AutoMigrate(&excelModel.SysExportTemplate{}); err != nil { return err }\n}\nvar count int64\nif err := db.Model(&excelModel.SysExportTemplate{}).Count(&count).Error; err != nil { return err }\nif count > 0 { return nil }","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 = initExcelTemplate{}.InitializeData(ctx)\nif err != nil {\n    log.Printf(\"export template seed failed: %v\", errors.Cause(err))\n    return err\n}","preventionTips":["Run AutoMigrate before the initializer chain","Guard seeding with DataInserted / row-count checks for idempotency","Verify DB writability and SQL-mode constraints before init","Initialize only once per database; never re-run on a seeded DB"],"tags":["gorm","database","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"}