flipped-aurora/gin-vue-admin · error

查询自动代码菜单 %s 失败: %w

Error message

查询自动代码菜单 %s 失败: %w

What it means

Thrown by persistAutoCodeMenu in the auto-code persistence service when the GORM query looking up an existing menu with the same name (for idempotent re-creation of auto-generated code menus) fails with an error other than gorm.ErrRecordNotFound. The code only tolerates 'not found' (meaning it should create a new menu); any other DB error is wrapped and returned, aborting the transaction.

Source

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

	var existing model.SysBaseMenu
	err := tx.Where("name = ?", desired.Name).First(&existing).Error
	switch {
	case err == nil:
		if existing.Name != desired.Name || existing.Path != desired.Path || existing.Component != desired.Component {
			return fmt.Errorf(
				"%w: name=%s, 已存在 path=%s component=%s, 期望 path=%s component=%s",
				errAutoCodeMenuConflict,
				desired.Name,
				existing.Path,
				existing.Component,
				desired.Path,
				desired.Component,
			)
		}
		history.MenuID = existing.ID
		return nil
	case !errors.Is(err, gorm.ErrRecordNotFound):
		return fmt.Errorf("查询自动代码菜单 %s 失败: %w", desired.Name, err)
	}

	if info.AutoCreateBtnAuth && !info.OnlyTemplate {
		desired.MenuBtn = []model.SysBaseMenuBtn{
			{Name: "add", Desc: "新增"},
			{Name: "batchDelete", Desc: "批量删除"},
			{Name: "delete", Desc: "删除"},
			{Name: "edit", Desc: "编辑"},
			{Name: "info", Desc: "详情"},
		}
		if info.HasExcel {
			desired.MenuBtn = append(desired.MenuBtn,
				model.SysBaseMenuBtn{Name: "exportTemplate", Desc: "导出模板"},
				model.SysBaseMenuBtn{Name: "exportExcel", Desc: "导出Excel"},
				model.SysBaseMenuBtn{Name: "importExcel", Desc: "导入Excel"},
			)
		}
	}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Check DB connectivity and that sys_base_menu exists (run migrations / AutoMigrate).
  2. Read the wrapped %w cause in the log to identify the driver error (e.g. 'no such table', 'connection refused').
  3. Verify the request's BusinessDB/package config points at the database that actually hosts the menu table.
  4. Retry the auto-code creation once connectivity is restored; the operation is transactional so no partial menu rows remain.
Defensive patterns

Strategy: try-catch

Validate before calling

// Go: verify DB reachable and table exists before generation
if err := db.Exec("SELECT 1 FROM sys_base_menu LIMIT 1").Error; err != nil {
    return fmt.Errorf("sys_base_menu unavailable: %w", err)
}

Try / catch

err := createAutoCode(req)
var dbErr *gorm.DBError
if errors.As(err, &dbErr) {
    // surface wrapped cause: fmt.Sprintf("%v", errors.Unwrap(err))
    retryAfterReconnect()
}

Prevention

When it happens

Trigger: Calling Create (auto code) with AutoCreateApi/AutoRemoveCreatedMenu flows where the sys_base_menu SELECT for desired.Name fails: DB connection drop, table missing/corrupted, context cancellation, or a driver-level error during the First/Where lookup.

Common situations: Database restarted or connection pool exhausted mid-request; sys_base_menu table not migrated (fresh DB without AutoMigrate); wrong BusinessDB configured so the query hits a database that lacks the menu table; row lock/timeout under concurrent menu writes.

Related errors


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