flipped-aurora/gin-vue-admin · error

存在重复name,请修改name

Error message

存在重复name,请修改name

What it means

Raised inside AddBaseMenu's transaction: before inserting a new base menu, the service checks whether another SysBaseMenu row already has the same route name. Because menu Name is used as the route identifier, duplicates are rejected with this ad-hoc error and the transaction rolls back.

Source

Thrown at server/service/system/sys_menu.go:141

func (menuService *MenuService) getBaseChildrenList(menu *system.SysBaseMenu, treeMap map[uint][]system.SysBaseMenu) (err error) {
	menu.Children = treeMap[menu.ID]
	for i := 0; i < len(menu.Children); i++ {
		err = menuService.getBaseChildrenList(&menu.Children[i], treeMap)
	}
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: AddBaseMenu
//@description: 添加基础路由
//@param: menu model.SysBaseMenu
//@return: error

func (menuService *MenuService) AddBaseMenu(ctx context.Context, menu system.SysBaseMenu) error {
	return global.GVA_DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
		// 检查name是否重复
		if !errors.Is(tx.Where("name = ?", menu.Name).First(&system.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) {
			return errors.New("存在重复name,请修改name")
		}

		if menu.ParentId != 0 {
			// 检查父菜单是否存在
			var parentMenu system.SysBaseMenu
			if err := tx.First(&parentMenu, menu.ParentId).Error; err != nil {
				if errors.Is(err, gorm.ErrRecordNotFound) {
					return errors.New("父菜单不存在")
				}
				return err
			}

			// 检查父菜单下现有子菜单数量
			var existingChildrenCount int64
			err := tx.Model(&system.SysBaseMenu{}).Where("parent_id = ?", menu.ParentId).Count(&existingChildrenCount).Error
			if err != nil {
				return err
			}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Pick a unique Name for the new menu (query SELECT name FROM sys_base_menu to see taken names).
  2. If the menu already exists, use the update API instead of adding a new one.
  3. Remove duplicate rows from import/seed data before re-running.

Example fix

// before
menuApi.addMenu({ name: 'user', ... })  // 'user' already exists
// after
menuApi.addMenu({ name: 'userProfile', ... })
Defensive patterns

Strategy: validation

Validate before calling

const names = (await menuApi.getMenuList()).data.list.map(m => m.name)
if (names.includes(newMenu.name)) {
  alert(`菜单 name "${newMenu.name}" 已存在,请修改后再提交`)
  return
}

Try / catch

try {
  await menuApi.addMenu(newMenu)
} catch (e) {
  if (String(e.msg).includes('存在重复name')) {
    formRef.value.validateField('name') // highlight the name field for editing
  } else throw e
}

Prevention

When it happens

Trigger: POST /menu/addMenu (or any caller of MenuService.AddBaseMenu) with a menu whose Name matches an existing menu row in sys_base_menu.

Common situations: Re-submitting a form twice; importing seed data that already created menus; copying a menu and forgetting to change the name; syncing menus from another environment with the same route names.

Related errors


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