flipped-aurora/gin-vue-admin · error

查询用户数据失败

Error message

查询用户数据失败

What it means

SetUserAuthorities begins a transaction by re-fetching the target user (tx.Where("id = ?", id).First(&user)). If that query errors, the raw DB error is logged at debug level and the generic errors.New("查询用户数据失败") is returned to the caller, hiding the underlying cause.

Source

Thrown at server/service/system/sys_user.go:205

	}

	err = global.GVA_DB.WithContext(ctx).Model(&system.SysUser{}).Where("id = ?", id).Update("authority_id", authorityId).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: SetUserAuthorities
//@description: 设置一个用户的权限
//@param: id uint, authorityIds []string
//@return: err error

func (userService *UserService) SetUserAuthorities(ctx context.Context, adminAuthorityID, id uint, authorityIds []uint) (err error) {
	return global.GVA_DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
		var user system.SysUser
		TxErr := tx.Where("id = ?", id).First(&user).Error
		if TxErr != nil {
			logger.WithCtx(ctx).Mod("biz").Debug(TxErr.Error())
			return errors.New("查询用户数据失败")
		}
		TxErr = tx.Delete(&[]system.SysUserAuthority{}, "sys_user_id = ?", id).Error
		if TxErr != nil {
			return TxErr
		}
		var useAuthority []system.SysUserAuthority
		for _, v := range authorityIds {
			e := AuthorityServiceApp.CheckAuthorityIDAuth(ctx, adminAuthorityID, v)
			if e != nil {
				return e
			}
			useAuthority = append(useAuthority, system.SysUserAuthority{
				SysUserId: id, SysAuthorityAuthorityId: v,
			})
		}
		TxErr = tx.Create(&useAuthority).Error
		if TxErr != nil {
			return TxErr

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Confirm the user id exists: SELECT id FROM sys_user WHERE id = ?.
  2. Reload the user list in the front end and retry with a fresh id.
  3. Check debug logs (logger.WithCtx debug line) for the wrapped gorm error to distinguish not-found vs connection failure.
  4. Verify migrations ran (sys_user table exists) if all ids fail.

Example fix

// before
await setUserAuthorities({ id: deletedUserId, authorityIds })
// after
const user = await getUserById(id); if (!user) return; await setUserAuthorities({ id: user.id, authorityIds })
Defensive patterns

Strategy: try-catch

Validate before calling

// verify target user exists before calling
const exists = await getUserById(id); if (!exists) return

Try / catch

try {
  await setUserAuthorities({ id, authorityIds })
} catch (e) {
  if (e.message === '查询用户数据失败') { await refreshUserList(); ElMessage.error('用户不存在或已被删除') }
  else throw e
}

Prevention

When it happens

Trigger: Calling SetUserAuthorities (sys_user.go:205) with a user id that does not exist in sys_user, or when the underlying SELECT fails (DB connection issue, table missing, context cancelled).

Common situations: Stale front-end data operating on a deleted user; passing 0 or a wrong id from the API layer; database connectivity/migration problems in dev environments.

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/2e2a053dc069fe00. Report an issue: GitHub.