flipped-aurora/gin-vue-admin · error

该角色不存在

Error message

该角色不存在

What it means

DeleteAuthority loads the role (with preloaded Users) before deleting; if the First() returns gorm.ErrRecordNotFound it returns "该角色不存在". The role cannot be deleted because it does not exist in sys_authorities at the time of the call.

Source

Thrown at server/service/system/sys_authority.go:136

	var oldAuthority system.SysAuthority
	err = global.GVA_DB.WithContext(ctx).Where("authority_id = ?", auth.AuthorityId).First(&oldAuthority).Error
	if err != nil {
		logger.WithCtx(ctx).Mod("biz").Debug(err.Error())
		return system.SysAuthority{}, errors.New("查询角色数据失败")
	}
	err = global.GVA_DB.WithContext(ctx).Model(&oldAuthority).Updates(&auth).Error
	return auth, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteAuthority
//@description: 删除角色
//@param: auth *model.SysAuthority
//@return: err error

func (authorityService *AuthorityService) DeleteAuthority(ctx context.Context, auth *system.SysAuthority) error {
	if errors.Is(global.GVA_DB.WithContext(ctx).Preload("Users").First(&auth).Error, gorm.ErrRecordNotFound) {
		return errors.New("该角色不存在")
	}
	if len(auth.Users) != 0 {
		return errors.New("此角色有用户正在使用禁止删除")
	}
	if !errors.Is(global.GVA_DB.WithContext(ctx).Where("authority_id = ?", auth.AuthorityId).First(&system.SysUser{}).Error, gorm.ErrRecordNotFound) {
		return errors.New("此角色有用户正在使用禁止删除")
	}
	if !errors.Is(global.GVA_DB.WithContext(ctx).Where("parent_id = ?", auth.AuthorityId).First(&system.SysAuthority{}).Error, gorm.ErrRecordNotFound) {
		return errors.New("此角色存在子角色不允许删除")
	}

	return global.GVA_DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
		var err error
		if err = tx.Preload("SysBaseMenus").Where("authority_id = ?", auth.AuthorityId).First(auth).Unscoped().Delete(auth).Error; err != nil {
			return err
		}

		if len(auth.SysBaseMenus) > 0 {

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Refresh the role list and confirm the role is already gone — no action needed.
  2. Verify the authorityId in the request matches an existing role.
  3. Check you are connected to the intended database/environment.

Example fix

// before: blind delete that can double-fire
await Promise.all(ids.map(id => deleteAuthority({ authorityId: id })))

// after: dedupe and tolerate already-deleted
await Promise.all([...new Set(ids)].map(async id => {
  try { await deleteAuthority({ authorityId: id }) } catch (e) {
    if (!String(e.msg).includes('该角色不存在')) throw e
  }
}))
Defensive patterns

Strategy: try-catch

Validate before calling

const roles = (await getAuthorityList()).data.list
if (!roles.some(r => r.authorityId === id)) throw new Error('角色已不存在,跳过删除')

Try / catch

try {
  await deleteAuthority({ authorityId: id })
} catch (e) {
  if (String(e?.msg).includes('该角色不存在')) {
    ElMessage.info('角色已被删除,无需重复操作')
    refreshList()
  } else { throw e }
}

Prevention

When it happens

Trigger: DELETE /authority/deleteAuthority with an authorityId absent from sys_authorities — double-delete (same role removed twice), deleting an already-deleted role from a stale page, or deleting a role from the wrong environment/database.

Common situations: Concurrent admins deleting the same role; frontend list not refreshed after another deletion; scripts replaying deletion requests; environment mismatch (staging ID used against production DB).

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


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