flipped-aurora/gin-vue-admin · error
创建失败!
Error message
创建失败!
What it means
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.
Source
Thrown at server/service/system/auto_code_history.go:34
common "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
request "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
"github.com/flipped-aurora/gin-vue-admin/server/utils"
"github.com/flipped-aurora/gin-vue-admin/server/utils/logger"
)
var AutocodeHistory = new(autoCodeHistory)
type autoCodeHistory struct{}
// Create 创建代码生成器历史记录
// Author [SliverHorn](https://github.com/SliverHorn)
// Author [songzhibin97](https://github.com/songzhibin97)
func (s *autoCodeHistory) Create(ctx context.Context, info request.SysAutoHistoryCreate) error {
create := info.Create()
err := global.GVA_DB.WithContext(ctx).Create(&create).Error
if err != nil {
return errors.Wrap(err, "创建失败!")
}
return nil
}
// First 根据id获取代码生成器历史的数据
// Author [SliverHorn](https://github.com/SliverHorn)
// Author [songzhibin97](https://github.com/songzhibin97)
func (s *autoCodeHistory) First(ctx context.Context, info common.GetById) (string, error) {
var meta string
err := global.GVA_DB.WithContext(ctx).Model(model.SysAutoCodeHistory{}).Where("id = ?", info.ID).Pluck("request", &meta).Error
if err != nil {
return "", errors.Wrap(err, "获取失败!")
}
return meta, nil
}
// Repeat 检测重复
// Author [SliverHorn](https://github.com/SliverHorn)View on GitHub (pinned to 3136500ef3)
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.
Example fix
// before
err := s.Create(ctx, info)
log.Println(err) // "创建失败!" with no detail
// after
if err := s.Create(ctx, info); err != nil {
log.Printf("history create failed: %+v", err) // prints wrapped DB cause
} Defensive patterns
Strategy: try-catch
Validate before calling
// pre-check table existence before writing history
hasTable := global.GVA_DB.Migrator().HasTable("sys_auto_code_history")
if !hasTable {
return errors.New("sys_auto_code_history table missing; run migrations first")
} Try / catch
if err := s.Create(ctx, info); err != nil {
var dbErr error
if errors.As(err, &dbErr) {
log.Printf("history insert failed: %+v", dbErr) // pkg/errors chain shows root cause
}
return err // do not swallow; autocode record loss matters
} Prevention
- 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
When it happens
Trigger: Calling Create(ctx, info) when the GORM insert fails: table missing (no AutoMigrate), connection failure, constraint violation, or invalid/oversized fields in request.SysAutoHistoryCreate.
Common situations: 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).
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/dddbf08271146bad.
Report an issue: GitHub.