{"record":{"id":"5fb8022e82122c0c","repo":"flipped-aurora/gin-vue-admin","slug":"error-5fb802","errorCode":null,"errorMessage":"该岗位下存在用户,不允许删除","messagePattern":"该岗位下存在用户,不允许删除","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_position.go","lineNumber":46,"sourceCode":"\t\t\"name\":   pos.Name,\n\t\t\"code\":   pos.Code,\n\t\t\"sort\":   pos.Sort,\n\t\t\"status\": pos.Status,\n\t\t\"remark\": pos.Remark,\n\t}).Error\n}\n\n// DeleteSysPosition 删除岗位, 已有用户绑定时禁止删除\nfunc (s *SysPositionService) DeleteSysPosition(ctx context.Context, id uint) (err error) {\n\tif id == 0 {\n\t\treturn errors.New(\"岗位ID不能为空\")\n\t}\n\tvar joinCount int64\n\tif err = global.GVA_DB.WithContext(ctx).Model(&system.SysUserPosition{}).Where(\"sys_position_id = ?\", id).Count(&joinCount).Error; err != nil {\n\t\treturn err\n\t}\n\tif joinCount > 0 {\n\t\treturn errors.New(\"该岗位下存在用户,不允许删除\")\n\t}\n\treturn global.GVA_DB.WithContext(ctx).Delete(&system.SysPosition{}, id).Error\n}\n\n// GetSysPosition 获取单个岗位\nfunc (s *SysPositionService) GetSysPosition(ctx context.Context, id uint) (pos system.SysPosition, err error) {\n\terr = global.GVA_DB.WithContext(ctx).First(&pos, id).Error\n\treturn\n}\n\n// GetSysPositionList 分页获取岗位列表\nfunc (s *SysPositionService) GetSysPositionList(ctx context.Context, info systemReq.SysPositionSearch) (list []system.SysPosition, total int64, err error) {\n\tlimit := info.PageSize\n\toffset := info.PageSize * (info.Page - 1)\n\tdb := global.GVA_DB.WithContext(ctx).Model(&system.SysPosition{})\n\tif info.Name != \"\" {\n\t\tdb = db.Where(\"name LIKE ?\", \"%\"+info.Name+\"%\")\n\t}","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_position.go#L28-L64","documentation":"DeleteSysPosition counts rows in sys_user_position referencing the position; if any user is still bound to it, the delete is refused with this error to avoid orphaning user-position assignments. The delete only proceeds when joinCount is 0.","triggerScenarios":"Calling DELETE /position/deletePosition for a position id that has at least one row in sys_user_position (i.e. at least one user assigned to that position).","commonSituations":"Trying to clean up obsolete positions that are still assigned to employees; deleting via script without checking bindings; cascading org restructures where users were not reassigned first.","solutions":["Unassign users from the position first (SetPositionUsers with an empty/adjusted list, or remove user-position bindings).","Query bindings to see affected users: SELECT * FROM sys_user_position WHERE sys_position_id = <id>.","Reassign affected users to a replacement position, then retry the delete."],"exampleFix":"// before\nawait positionApi.deletePosition(id) // fails: users still bound\n// after\nconst users = await positionApi.getPositionUsers(id)\nawait positionApi.setPositionUsers({ positionId: id, userIds: [] }) // clear bindings\nawait positionApi.deletePosition(id)","handlingStrategy":"validation","validationCode":"const users = (await positionApi.getPositionUsers(id)).data || []\nif (users.length > 0) {\n  if (!confirm(`该岗位仍有 ${users.length} 名用户，需先解除绑定，是否继续清空并删除？`)) return\n  await positionApi.setPositionUsers({ positionId: id, userIds: [] })\n}\nawait positionApi.deletePosition({ id })","typeGuard":null,"tryCatchPattern":"try {\n  await positionApi.deletePosition({ id })\n} catch (e) {\n  if (e.msg === '该岗位下存在用户,不允许删除') {\n    showUnassignUsersDialog(id) // walk user through clearing bindings first\n  } else throw e\n}","preventionTips":["Check user bindings before offering delete in the UI.","Provide a reassign-users workflow in org restructuring plans.","Prefer deactivating (status=0) obsolete positions over deleting them."],"tags":["go","gin-vue-admin","position","referential-integrity"],"backgroundTag":"record-in-use","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}