{"record":{"id":"2e2a053dc069fe00","repo":"flipped-aurora/gin-vue-admin","slug":"error-2e2a05","errorCode":null,"errorMessage":"查询用户数据失败","messagePattern":"查询用户数据失败","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_user.go","lineNumber":205,"sourceCode":"\t}\n\n\terr = global.GVA_DB.WithContext(ctx).Model(&system.SysUser{}).Where(\"id = ?\", id).Update(\"authority_id\", authorityId).Error\n\treturn err\n}\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@function: SetUserAuthorities\n//@description: 设置一个用户的权限\n//@param: id uint, authorityIds []string\n//@return: err error\n\nfunc (userService *UserService) SetUserAuthorities(ctx context.Context, adminAuthorityID, id uint, authorityIds []uint) (err error) {\n\treturn global.GVA_DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {\n\t\tvar user system.SysUser\n\t\tTxErr := tx.Where(\"id = ?\", id).First(&user).Error\n\t\tif TxErr != nil {\n\t\t\tlogger.WithCtx(ctx).Mod(\"biz\").Debug(TxErr.Error())\n\t\t\treturn errors.New(\"查询用户数据失败\")\n\t\t}\n\t\tTxErr = tx.Delete(&[]system.SysUserAuthority{}, \"sys_user_id = ?\", id).Error\n\t\tif TxErr != nil {\n\t\t\treturn TxErr\n\t\t}\n\t\tvar useAuthority []system.SysUserAuthority\n\t\tfor _, v := range authorityIds {\n\t\t\te := AuthorityServiceApp.CheckAuthorityIDAuth(ctx, adminAuthorityID, v)\n\t\t\tif e != nil {\n\t\t\t\treturn e\n\t\t\t}\n\t\t\tuseAuthority = append(useAuthority, system.SysUserAuthority{\n\t\t\t\tSysUserId: id, SysAuthorityAuthorityId: v,\n\t\t\t})\n\t\t}\n\t\tTxErr = tx.Create(&useAuthority).Error\n\t\tif TxErr != nil {\n\t\t\treturn TxErr","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_user.go#L187-L223","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Confirm the user id exists: SELECT id FROM sys_user WHERE id = ?.","Reload the user list in the front end and retry with a fresh id.","Check debug logs (logger.WithCtx debug line) for the wrapped gorm error to distinguish not-found vs connection failure.","Verify migrations ran (sys_user table exists) if all ids fail."],"exampleFix":"// before\nawait setUserAuthorities({ id: deletedUserId, authorityIds })\n// after\nconst user = await getUserById(id); if (!user) return; await setUserAuthorities({ id: user.id, authorityIds })","handlingStrategy":"try-catch","validationCode":"// verify target user exists before calling\nconst exists = await getUserById(id); if (!exists) return","typeGuard":null,"tryCatchPattern":"try {\n  await setUserAuthorities({ id, authorityIds })\n} catch (e) {\n  if (e.message === '查询用户数据失败') { await refreshUserList(); ElMessage.error('用户不存在或已被删除') }\n  else throw e\n}","preventionTips":["Refresh user lists after deletions to avoid stale ids.","Never pass hard-coded or zero user ids.","Check debug logs for the wrapped gorm error when diagnosing."],"tags":["gorm","user","transaction","not-found"],"backgroundTag":"record-not-found","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}