flipped-aurora/gin-vue-admin · error
创建自动代码 API %s %s 失败: %w
Error message
创建自动代码 API %s %s 失败: %w
What it means
This error is returned by persistAutoCodeAPIs in server/service/system/auto_code_persistence.go when a desired sys_api row (path/method not found in DB) fails to insert via tx.Create. It wraps the GORM error with the API's method and path for identification and aborts the surrounding transaction, so no partial API/history state is persisted.
Source
Thrown at server/service/system/auto_code_persistence.go:56
return fmt.Errorf("创建自动代码历史失败: %w", err)
}
return nil
})
}
func persistAutoCodeAPIs(tx *gorm.DB, info request.AutoCode, history *request.SysAutoHistoryCreate) error {
if !info.AutoCreateApiToSql || info.OnlyTemplate {
return nil
}
for _, desired := range info.Apis() {
var existing model.SysApi
err := tx.Where("path = ? AND method = ?", desired.Path, desired.Method).First(&existing).Error
switch {
case err == nil:
history.ApiIDs = append(history.ApiIDs, existing.ID)
case errors.Is(err, gorm.ErrRecordNotFound):
if err = tx.Create(&desired).Error; err != nil {
return fmt.Errorf("创建自动代码 API %s %s 失败: %w", desired.Method, desired.Path, err)
}
history.ApiIDs = append(history.ApiIDs, desired.ID)
default:
return fmt.Errorf("查询自动代码 API %s %s 失败: %w", desired.Method, desired.Path, err)
}
}
return nil
}
func persistAutoCodeMenu(
tx *gorm.DB,
info request.AutoCode,
packageTemplate string,
history *request.SysAutoHistoryCreate,
) error {
if !info.AutoCreateMenuToSql {
return nil
}View on GitHub (pinned to 3136500ef3)
Solutions
- Unwrap the error to identify the exact DB failure (duplicate key, unknown column, connection error).
- For creation races, use an upsert (ON CONFLICT/ON DUPLICATE KEY) or catch duplicate-key errors and re-query to attach the existing ID to history.ApiIDs.
- Run migrations/AutoMigrate so sys_api matches the current model.
- Validate desired fields (path, method, apiGroup, description) are non-empty and within column limits before insert.
- Retry the full persistence transaction on transient DB errors.
Example fix
// before
if err = tx.Create(&desired).Error; err != nil {
return fmt.Errorf("创建自动代码 API %s %s 失败: %w", desired.Method, desired.Path, err)
}
// after
if err = tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&desired).Error; err != nil {
return fmt.Errorf("创建自动代码 API %s %s 失败: %w", desired.Method, desired.Path, err)
}
var existing system.SysApi
if err := tx.Where("path = ? AND method = ?", desired.Path, desired.Method).First(&existing).Error; err == nil {
history.ApiIDs = append(history.ApiIDs, existing.ID)
} Defensive patterns
Strategy: validation
Validate before calling
func apiExists(db *gorm.DB, path, method string) (uint, bool, error) {
var api system.SysApi
err := db.Where("path = ? AND method = ?", path, method).First(&api).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return 0, false, nil
}
if err != nil {
return 0, false, err
}
return api.ID, true, nil
}
// call before the persistence flow to pre-check each desired API
Try / catch
err := svc.CreateAutoCodeHistory(info)
if err != nil {
var dupErr *mysql.MySQLError
if errors.As(errors.Unwrap(err), &dupErr) && dupErr.Number == 1062 {
return retryWithBackoff(2, func() error { return svc.CreateAutoCodeHistory(info) })
}
return fmt.Errorf("persist auto code APIs failed: %w", err)
} Prevention
- Pre-check desired path/method pairs against sys_api before running persistence.
- Serialize code-generation jobs per package (lock or queue) to avoid concurrent insert races on sys_api.
- Keep sys_api schema migrated and validate required fields (path, method, apiGroup) before insert.
- Enable idempotent persistence (upsert or find-or-create) for repeated generation runs.
- Wrap persistence in one transaction and retry the whole unit on duplicate-key or transient errors.
When it happens
Trigger: Persisting auto-code whose API definitions include a path+method pair absent from sys_api, and the INSERT fails: unique-index collision from a concurrent creation of the same path/method, NOT NULL columns missing in the desired struct, DB connection failure, or schema drift renaming/removing sys_api columns.
Common situations: Two concurrent code-generation runs racing to insert the same API row; partial migrations leaving sys_api without required columns; over-length or invalid characters in path/description failing strict SQL mode; read-only database or full disk.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/35074a9515a4c166.
Report an issue: GitHub.