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 when the tx.Where("path = ? AND method = ?").First(&existing) existence check on sys_api fails with an error other than gorm.ErrRecordNotFound (which is handled as the create-new branch). The code cannot determine whether the API exists, so it fails the transaction, including the method and path in the message for diagnosis.

Source

Thrown at server/service/system/auto_code_persistence.go:60

}

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
	}
	desired := info.Menu(packageTemplate)
	var existing model.SysBaseMenu
	err := tx.Where("name = ?", desired.Name).First(&existing).Error
	switch {

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Unwrap the error to read the exact DB message (no such table, unknown column, access denied, lock timeout).
  2. Run migrations/AutoMigrate so sys_api exists with path/method columns.
  3. Grant the DB user SELECT privileges on sys_api if access denied.
  4. Retry the persistence operation if the cause is a lock timeout or transient network error.
  5. Shorten transaction scope to reduce lock contention if timeouts recur.

Example fix

// before
default:
    return fmt.Errorf("查询自动代码 API %s %s 失败: %w", desired.Method, desired.Path, err)

// after
default:
    logger.Error("sys_api lookup failed",
        zap.String("path", desired.Path), zap.String("method", desired.Method), zap.Error(err))
    return fmt.Errorf("查询自动代码 API %s %s 失败: %w", desired.Method, desired.Path, err)
Defensive patterns

Strategy: retry

Validate before calling

func sysApiTableReady(db *gorm.DB) error {
    return db.Exec("SELECT 1 FROM sys_api LIMIT 1").Error // surfaces missing table/privilege early
}

Try / catch

err := svc.CreateAutoCodeHistory(info)
if err != nil {
    if isTransientDBError(err) { // lock wait timeout, connection reset, deadlock
        return retryWithBackoff(3, func() error { return svc.CreateAutoCodeHistory(info) })
    }
    if strings.Contains(fmt.Sprint(err), "doesn't exist") {
        return fmt.Errorf("schema not migrated; run AutoMigrate first: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: The SELECT on sys_api fails during auto-code persistence: DB connection dropped mid-transaction, sys_api table missing (migrations not run), unknown-column/schema-drift SQL errors, deadlock or innodb_lock_wait_timeout, or the DB user lacks SELECT privilege on sys_api.

Common situations: Fresh deployments where AutoMigrate has not created sys_api; DB user with insufficient privileges; schema mismatch after upgrading gin-vue-admin without migrating; transient network blips between app and DB; long-running transactions hitting lock wait timeouts.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/975a3d0ce21024be. Report an issue: GitHub.