flipped-aurora/gin-vue-admin · error
存在子部门,不允许删除
Error message
存在子部门,不允许删除
What it means
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.
Source
Thrown at server/service/system/sys_department.go:131
return err
}
return global.GVA_DB.WithContext(ctx).Model(&system.SysDepartment{}).Where("id = ?", dept.ID).Updates(map[string]interface{}{
"name": dept.Name,
"parent_id": dept.ParentId,
"ancestors": ancestors,
"sort": dept.Sort,
"leader_id": dept.LeaderId,
"status": dept.Status,
}).Error
}
// DeleteSysDepartment 删除部门, 存在子部门或已有用户归属时禁止删除
func (s *SysDepartmentService) DeleteSysDepartment(ctx context.Context, id uint) (err error) {
if id == 0 {
return errors.New("部门ID不能为空")
}
if !errors.Is(global.GVA_DB.WithContext(ctx).Where("parent_id = ?", id).First(&system.SysDepartment{}).Error, gorm.ErrRecordNotFound) {
return errors.New("存在子部门,不允许删除")
}
var userCount int64
if err = global.GVA_DB.WithContext(ctx).Model(&system.SysUser{}).Where("dept_id = ?", id).Count(&userCount).Error; err != nil {
return err
}
if userCount > 0 {
return errors.New("该部门下存在用户,不允许删除")
}
var joinCount int64
if err = global.GVA_DB.WithContext(ctx).Model(&system.SysUserDepartment{}).Where("sys_department_id = ?", id).Count(&joinCount).Error; err != nil {
return err
}
if joinCount > 0 {
return errors.New("该部门下存在用户,不允许删除")
}
return global.GVA_DB.WithContext(ctx).Delete(&system.SysDepartment{}, id).Error
}
View on GitHub (pinned to 3136500ef3)
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
Defensive patterns
Strategy: validation
Validate before calling
var child system.SysDepartment
err := global.GVA_DB.Where("parent_id = ?", id).First(&child).Error
if !errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("delete or move child departments first")
} Try / catch
if err := deptService.DeleteSysDepartment(ctx, id); err != nil {
if err.Error() == "存在子部门,不允许删除" {
// show tree so user can remove children first
}
return err
} Prevention
- 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
When it happens
Trigger: Deleting a department that has at least one direct child in sys_department via DeleteSysDepartment or the delete API.
Common situations: 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.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/8e841d1d94712714.
Report an issue: GitHub.