{"record":{"id":"dddbf08271146bad","repo":"flipped-aurora/gin-vue-admin","slug":"error-dddbf0","errorCode":null,"errorMessage":"创建失败!","messagePattern":"创建失败!","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/auto_code_history.go","lineNumber":34,"sourceCode":"\tcommon \"github.com/flipped-aurora/gin-vue-admin/server/model/common/request\"\n\tmodel \"github.com/flipped-aurora/gin-vue-admin/server/model/system\"\n\trequest \"github.com/flipped-aurora/gin-vue-admin/server/model/system/request\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/utils\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/utils/logger\"\n)\n\nvar AutocodeHistory = new(autoCodeHistory)\n\ntype autoCodeHistory struct{}\n\n// Create 创建代码生成器历史记录\n// Author [SliverHorn](https://github.com/SliverHorn)\n// Author [songzhibin97](https://github.com/songzhibin97)\nfunc (s *autoCodeHistory) Create(ctx context.Context, info request.SysAutoHistoryCreate) error {\n\tcreate := info.Create()\n\terr := global.GVA_DB.WithContext(ctx).Create(&create).Error\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"创建失败!\")\n\t}\n\treturn nil\n}\n\n// First 根据id获取代码生成器历史的数据\n// Author [SliverHorn](https://github.com/SliverHorn)\n// Author [songzhibin97](https://github.com/songzhibin97)\nfunc (s *autoCodeHistory) First(ctx context.Context, info common.GetById) (string, error) {\n\tvar meta string\n\terr := global.GVA_DB.WithContext(ctx).Model(model.SysAutoCodeHistory{}).Where(\"id = ?\", info.ID).Pluck(\"request\", &meta).Error\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"获取失败!\")\n\t}\n\treturn meta, nil\n}\n\n// Repeat 检测重复\n// Author [SliverHorn](https://github.com/SliverHorn)","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/auto_code_history.go#L16-L52","documentation":"autoCodeHistory.Create persists a code-generator history record via GORM (global.GVA_DB...Create). If the insert fails, the raw DB error is wrapped with errors.Wrap(err, \"创建失败!\") so the caller sees \"创建失败!\" plus the underlying cause in the wrap chain. It indicates the history record could not be written to the sys_auto_code_history table.","triggerScenarios":"Calling Create(ctx, info) when the GORM insert fails: table missing (no AutoMigrate), connection failure, constraint violation, or invalid/oversized fields in request.SysAutoHistoryCreate.","commonSituations":"Fresh environment where migrations haven't created the history table; DB credentials/network misconfigured; dropped column after schema change; data too long for column (long code snippets exceeding TEXT limits).","solutions":["Unwrap the returned error (%+v with pkg/errors) to see the underlying GORM/MySQL cause.","Ensure the sys_auto_code_history table exists — run migrations/AutoMigrate in the environment.","Verify global.GVA_DB configuration (DSN, connectivity, permissions) is correct.","Check the payload fields against the table schema (column lengths, NOT NULL constraints) and trim oversized data."],"exampleFix":"// before\nerr := s.Create(ctx, info)\nlog.Println(err) // \"创建失败!\" with no detail\n\n// after\nif err := s.Create(ctx, info); err != nil {\n    log.Printf(\"history create failed: %+v\", err) // prints wrapped DB cause\n}","handlingStrategy":"try-catch","validationCode":"// pre-check table existence before writing history\nhasTable := global.GVA_DB.Migrator().HasTable(\"sys_auto_code_history\")\nif !hasTable {\n    return errors.New(\"sys_auto_code_history table missing; run migrations first\")\n}","typeGuard":null,"tryCatchPattern":"if err := s.Create(ctx, info); err != nil {\n    var dbErr error\n    if errors.As(err, &dbErr) {\n        log.Printf(\"history insert failed: %+v\", dbErr) // pkg/errors chain shows root cause\n    }\n    return err // do not swallow; autocode record loss matters\n}","preventionTips":["Run AutoMigrate for sys_auto_code_history on new environments","Unwrap pkg/errors chains to see the true DB cause, not just 创建失败!","Validate payload field lengths against the schema before insert","Monitor DB connectivity and credentials in deployment configs"],"tags":["gorm","database","autocode","persistence"],"backgroundTag":"db-insert-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}