flipped-aurora/gin-vue-admin · error
主部门必须在归属部门范围内
Error message
主部门必须在归属部门范围内
What it means
SetUserDepartments stamps the user's data-permission department (dept_id) with primaryDeptId. The primary department must be inside the deptIds set; if the loop over deptIds finds no match for pid, it returns errors.New("主部门必须在归属部门范围内") before updating.
Source
Thrown at server/service/system/sys_user.go:269
if txErr := tx.Create(&records).Error; txErr != nil {
return txErr
}
}
// 计算主部门
pid := primaryDeptId
if pid == 0 && len(deptIds) > 0 {
pid = deptIds[0]
}
if pid != 0 {
inSet := false
for _, deptId := range deptIds {
if deptId == pid {
inSet = true
break
}
}
if !inSet {
return errors.New("主部门必须在归属部门范围内")
}
}
return tx.Model(&system.SysUser{}).Where("id = ?", id).Update("dept_id", pid).Error
})
}
// SetUserPositions 设置用户岗位(多岗位)
func (userService *UserService) SetUserPositions(ctx context.Context, id uint, positionIds []uint) (err error) {
return global.GVA_DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if txErr := tx.Delete(&[]system.SysUserPosition{}, "sys_user_id = ?", id).Error; txErr != nil {
return txErr
}
if len(positionIds) > 0 {
records := make([]system.SysUserPosition, 0, len(positionIds))
for _, positionId := range positionIds {
records = append(records, system.SysUserPosition{SysUserId: id, SysPositionId: positionId})
}
if txErr := tx.Create(&records).Error; txErr != nil {View on GitHub (pinned to 3136500ef3)
Solutions
- Set primaryDeptId to one of the ids in deptIds, or pass 0 to let the service default to deptIds[0].
- Update the front end so unchecking the primary department clears/resets primaryDeptId.
- Ensure deptIds and primaryDeptId come from the same submitted form state, not mixed with cached data.
Example fix
// before
await setUserDepartments({ id, deptIds: [2,3], primaryDeptId: 7 }) // 7 not in set
// after
await setUserDepartments({ id, deptIds: [2,3], primaryDeptId: 2 }) Defensive patterns
Strategy: validation
Validate before calling
if (primaryDeptId && !deptIds.includes(primaryDeptId)) {
ElMessage.warning('主部门必须在归属部门范围内'); return
} Try / catch
try {
await setUserDepartments({ id, deptIds, primaryDeptId })
} catch (e) {
if (e.message === '主部门必须在归属部门范围内') ElMessage.warning('请选择归属部门内的主部门')
else throw e
} Prevention
- In the dept tree UI, prevent unchecking the current primary department without resetting primaryDeptId.
- Pass primaryDeptId = 0 to let the backend default to the first dept in the set.
- Keep deptIds and primaryDeptId sourced from the same form state.
When it happens
Trigger: Calling SetUserDepartments (sys_user.go:269) where primaryDeptId is non-zero but not present in deptIds — e.g. the user's current primary department was removed from the new department selection, or the caller passes a stale primaryDeptId.
Common situations: Front end keeping the old primary department checked-out while the user unchecks it in the dept tree; API consumers sending primaryDeptId from cached user data; forgetting to set primaryDeptId semantics (empty means first of set).
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/d8f81a62655b4bbf.
Report an issue: GitHub.