flipped-aurora/gin-vue-admin · error
部门ID不能为空
Error message
部门ID不能为空
What it means
DeleteSysDepartment requires a non-zero department ID. Passing 0 (Go's zero value for uint) is treated as 'no ID provided' and rejected before any DB access. This guards against deleting with an unset/unbound parameter.
Source
Thrown at server/service/system/sys_department.go:128
}
ancestors, err := s.buildAncestors(ctx, dept.ParentId)
if err != nil {
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("该部门下存在用户,不允许删除")
}View on GitHub (pinned to 3136500ef3)
Solutions
- Ensure the delete request carries the actual department ID (check the route param / request body)
- On the frontend, disable the delete action until the row's ID is available
- In code, check id != 0 before calling the service
Example fix
// before
deleteDepartment({ id: dept.id }) // dept.id may be undefined -> 0
// after
if (!dept.id) return
deleteDepartment({ id: dept.id }) Defensive patterns
Strategy: validation
Validate before calling
if id == 0 {
return errors.New("department id is required")
} Try / catch
if err := deptService.DeleteSysDepartment(ctx, id); err != nil {
if err.Error() == "部门ID不能为空" {
// fix request binding / ensure id is supplied
}
return err
} Prevention
- Always pass the row's real ID from a loaded record, not a fresh struct
- Verify gin route param binding (e.g. :id) matches the handler argument
- Disable delete buttons until row data with ID is loaded
When it happens
Trigger: DeleteSysDepartment(ctx, 0) — e.g. the API path param failed to bind, the frontend sent an empty id, or a struct's ID field was never populated before calling the service.
Common situations: Frontend calling delete before the row ID is loaded; gin URI/query binding mismatch so ID stays 0; tests invoking the service with a zero-value struct.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/05e6218cb024d226.
Report an issue: GitHub.