flipped-aurora/gin-vue-admin · error · errAutoCodeMenuConflict

%w: name=%s, 已存在 path=%s component=%s, 期望 path=%s component=

Error message

%w: name=%s, 已存在 path=%s component=%s, 期望 path=%s component=%s

What it means

This error is returned by persistAutoCodeMenu in server/service/system/auto_code_persistence.go when a sys_base_menu row with the same name already exists but its path or component differs from the menu the auto-code flow wants to persist. It wraps the sentinel errAutoCodeMenuConflict with a detailed diff (existing vs desired path/component) and fails the transaction, preventing an ambiguous silent overwrite of an existing menu definition.

Source

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

	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 {
	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: "新增"},

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Read the message fields (existing path/component vs desired path/component) and decide which definition is correct.
  2. Delete or rename the conflicting sys_base_menu entry in the menu manager, then re-run auto-code generation.
  3. If the existing menu is intended, align the packageTemplate/menu config so the desired path/component matches the existing row.
  4. If regeneration should win, back up and remove the old menu (and its children/parameters) before re-running.
  5. Handle errAutoCodeMenuConflict distinctly in the caller (errors.Is) to surface the conflict details to the user instead of a generic failure.

Example fix

// before
// conflict: existing menu 'user' path='user' component='view/user/user.vue',
// desired component changed to 'view/user/newUser.vue' -> generation aborts

// after (resolve conflict, then regenerate)
// In 系统管理 -> 菜单管理: rename or delete the conflicting 'user' menu,
// then re-run auto-code generation so persistAutoCodeMenu creates the menu
// with the new path/component without triggering errAutoCodeMenuConflict.
Defensive patterns

Strategy: validation

Validate before calling

func menuConflict(db *gorm.DB, name, path, component string) error {
    var m system.SysBaseMenu
    err := db.Where("name = ?", name).First(&m).Error
    if errors.Is(err, gorm.ErrRecordNotFound) {
        return nil
    }
    if err != nil {
        return err
    }
    if m.Path != path || m.Component != component {
        return fmt.Errorf("menu %q exists with path=%s component=%s, want path=%s component=%s",
            name, m.Path, m.Component, path, component)
    }
    return nil
}
// call before triggering auto-code generation/persistence

Try / catch

err := svc.CreateAutoCodeHistory(info)
if err != nil {
    if errors.Is(err, errAutoCodeMenuConflict) {
        return fmt.Errorf("menu conflict: rename/delete the existing menu or align packageTemplate, then regenerate")
    }
    return err
}

Prevention

When it happens

Trigger: Generating/persisting auto-code whose desired menu name collides with an existing menu registered under a different path or component — e.g. re-running generation after changing packageTemplate, moving a generated page to another directory (component path changed), or a hand-created/edited menu sharing the auto-generated name.

Common situations: Developer renamed a package/router but kept the old menu name; two codegen runs produced different component paths under the same menu name; a previously generated menu was manually edited in the menu manager and now conflicts with regeneration; leftover menus from earlier codegen attempts.

Related errors


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