{"record":{"id":"8e841d1d94712714","repo":"flipped-aurora/gin-vue-admin","slug":"error-8e841d","errorCode":null,"errorMessage":"存在子部门,不允许删除","messagePattern":"存在子部门,不允许删除","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_department.go","lineNumber":131,"sourceCode":"\t\treturn err\n\t}\n\treturn global.GVA_DB.WithContext(ctx).Model(&system.SysDepartment{}).Where(\"id = ?\", dept.ID).Updates(map[string]interface{}{\n\t\t\"name\":      dept.Name,\n\t\t\"parent_id\": dept.ParentId,\n\t\t\"ancestors\": ancestors,\n\t\t\"sort\":      dept.Sort,\n\t\t\"leader_id\": dept.LeaderId,\n\t\t\"status\":    dept.Status,\n\t}).Error\n}\n\n// DeleteSysDepartment 删除部门, 存在子部门或已有用户归属时禁止删除\nfunc (s *SysDepartmentService) DeleteSysDepartment(ctx context.Context, id uint) (err error) {\n\tif id == 0 {\n\t\treturn errors.New(\"部门ID不能为空\")\n\t}\n\tif !errors.Is(global.GVA_DB.WithContext(ctx).Where(\"parent_id = ?\", id).First(&system.SysDepartment{}).Error, gorm.ErrRecordNotFound) {\n\t\treturn errors.New(\"存在子部门,不允许删除\")\n\t}\n\tvar userCount int64\n\tif err = global.GVA_DB.WithContext(ctx).Model(&system.SysUser{}).Where(\"dept_id = ?\", id).Count(&userCount).Error; err != nil {\n\t\treturn err\n\t}\n\tif userCount > 0 {\n\t\treturn errors.New(\"该部门下存在用户,不允许删除\")\n\t}\n\tvar joinCount int64\n\tif err = global.GVA_DB.WithContext(ctx).Model(&system.SysUserDepartment{}).Where(\"sys_department_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.SysDepartment{}, id).Error\n}\n","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_department.go#L113-L149","documentation":"DeleteSysDepartment refuses to delete a department that still has child departments. It checks for any sys_department row with parent_id equal to the target ID; if found (i.e. the First query did NOT return record-not-found), it blocks deletion to preserve tree integrity.","triggerScenarios":"Deleting a department that has at least one direct child in sys_department via DeleteSysDepartment or the delete API.","commonSituations":"User deletes a parent node in the tree view that has collapsed children; API consumers deleting by ID without checking children; stale tree data hiding sub-departments.","solutions":["Delete or move all child departments first, then delete the target","In the UI, prevent delete on nodes with children or offer recursive delete with confirmation","Implement/approach a cascade or re-parenting strategy if the business allows it"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var child system.SysDepartment\nerr := global.GVA_DB.Where(\"parent_id = ?\", id).First(&child).Error\nif !errors.Is(err, gorm.ErrRecordNotFound) {\n    return errors.New(\"delete or move child departments first\")\n}","typeGuard":null,"tryCatchPattern":"if err := deptService.DeleteSysDepartment(ctx, id); err != nil {\n    if err.Error() == \"存在子部门,不允许删除\" {\n        // show tree so user can remove children first\n    }\n    return err\n}","preventionTips":["Expand/check children before deleting a tree node","Offer recursive delete with confirmation in the UI","Keep the department tree view in sync so hidden children are visible"],"tags":["department","referential-integrity","go"],"backgroundTag":"record-has-dependents","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}