flipped-aurora/gin-vue-admin · error
查询角色数据失败
Error message
查询角色数据失败
What it means
UpdateAuthority first loads the existing role by authority_id before applying Updates. If that First() query fails (role missing, or any DB error), it logs the underlying error at debug level and returns the generic "查询角色数据失败" to the caller. Note this masks the real cause — including transient DB failures — behind one message.
Source
Thrown at server/service/system/sys_authority.go:122
err = CasbinServiceApp.UpdateCasbin(ctx, adminAuthorityID, copyInfo.Authority.AuthorityId, paths)
if err != nil {
_ = authorityService.DeleteAuthority(ctx, ©Info.Authority)
}
return copyInfo.Authority, err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateAuthority
//@description: 更改一个角色
//@param: auth model.SysAuthority
//@return: authority system.SysAuthority, err error
func (authorityService *AuthorityService) UpdateAuthority(ctx context.Context, auth system.SysAuthority) (authority system.SysAuthority, err error) {
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("此角色有用户正在使用禁止删除")
}View on GitHub (pinned to 3136500ef3)
Solutions
- Confirm the role still exists (role management page or SELECT from sys_authorities) and use a valid authorityId.
- Refresh the frontend role list to clear stale IDs, then retry.
- Check server logs — the real gorm error is logged at debug level (logger.WithCtx(ctx).Mod("biz").Debug) before the generic message is returned.
Example fix
// before
updateAuthority({ authorityId: 7777, authorityName: 'ops' }) // 7777 was deleted
// after: re-fetch then update
const roles = await getAuthorityList()
const target = roles.data.list.find(r => r.authorityName === 'ops')
await updateAuthority({ authorityId: target.authorityId, authorityName: 'ops' }) Defensive patterns
Strategy: try-catch
Validate before calling
const roles = (await getAuthorityList()).data.list
if (!roles.some(r => r.authorityId === form.authorityId)) throw new Error('角色 ' + form.authorityId + ' 不存在,无法更新') Try / catch
try {
await updateAuthority(form)
} catch (e) {
if (String(e?.msg).includes('查询角色数据失败')) {
logger.debug('underlying gorm error', e)
ElMessage.error('角色不存在或查询失败:请刷新角色列表后重试')
} else { throw e }
} Prevention
- Refresh the role list before updates so payloads don't carry deleted IDs.
- Remember this error masks the real DB error — check server debug logs for the underlying cause.
- Handle concurrent deletions: re-fetch the role before editing in long-lived forms.
- Monitor DB connectivity/schema health if this error appears for IDs you know exist.
When it happens
Trigger: Calling PUT /authority/updateAuthority with an authorityId not present in sys_authorities; or the underlying SELECT fails due to DB connectivity/schema issues.
Common situations: Frontend holds a stale role list after the role was deleted in another tab/user; ID type mismatch in the payload; database down or table missing after a partial migration.
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/52238007eb81a688.
Report an issue: GitHub.