flipped-aurora/gin-vue-admin · warning

此角色有用户正在使用禁止删除

Error message

此角色有用户正在使用禁止删除

What it means

DeleteAuthority refuses to delete a role that still has users attached. After loading the role it checks len(auth.Users) from the preloaded Users relation; if any sys_users rows reference this authority via the join table, it returns "此角色有用户正在使用禁止删除". This is a referential-integrity guard preventing orphaned users.

Source

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

		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 {
			if err = tx.Model(auth).Association("SysBaseMenus").Delete(auth.SysBaseMenus); err != nil {
				return err
			}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Reassign the affected users to another role (update their role(s) in user management), then delete the role.
  2. List the users of the role first (role page shows bound users) and remove them from the role one by one.
  3. If the binding is stale, delete the orphan rows from the user_authority join table, then retry the deletion.

Example fix

// before
deleteAuthority({ authorityId: 888 }) // users still bound

// after: reassign users first
await setUserAuthorities({ userId: 5, authorityIds: [9528] })
await setUserAuthorities({ userId: 6, authorityIds: [9528] })
await deleteAuthority({ authorityId: 888 })
Defensive patterns

Strategy: validation

Validate before calling

const bound = (await getAuthorityList()).data.list.find(r => r.authorityId === id)?.users?.length
if (bound > 0) throw new Error('角色下仍有 ' + bound + ' 个用户,请先转移再删除')

Try / catch

try {
  await deleteAuthority({ authorityId: id })
} catch (e) {
  if (String(e?.msg).includes('此角色有用户正在使用禁止删除')) {
    ElMessage.warning('请先将该角色下的用户转移到其他角色')
  } else { throw e }
}

Prevention

When it happens

Trigger: DELETE /authority/deleteAuthority where the user_authority join table still contains rows for the role (Preload("Users") yields a non-empty slice).

Common situations: Decommissioning a role before offboarding its members; cleanup scripts that delete roles without reassigning users; imported datasets where join rows were preserved but roles were expected to be empty.

Related errors


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