flipped-aurora/gin-vue-admin · warning

primaryDeptId 必须属于合并后的部门集合

Error message

primaryDeptId %d 必须属于合并后的部门集合 %v

What it means

Validation error thrown by org_member_assigner Handle when the explicitly requested primaryDeptId is not a member of the final merged department set. The tool refuses to set a user's primary department to one they would not belong to after the merge of current and new department IDs, because the upstream setUserDepartments API would reject or inconsistently store such a primary.

Solutions

  1. Add the primaryDeptId value to the deptIDs array in the same tool call so it lands in the merged set
  2. Use primaryDeptId=0 (or omit it) to keep the user's existing primary department
  3. Verify the department ID exists via the org query tool and re-check for typos

Example fix

// before
{"userId": 5, "deptIds": [2,3], "primaryDeptId": 9}
// after
{"userId": 5, "deptIds": [2,3,9], "primaryDeptId": 9}
Defensive patterns

Strategy: validation

Validate before calling

func canAssign(deptIDs, merged []uint, primaryDeptId uint) error {
    if primaryDeptId == 0 {
        return nil
    }
    for _, id := range merged {
        if id == primaryDeptId {
            return nil
        }
    }
    return fmt.Errorf("primaryDeptId %d not in merged dept set %v", primaryDeptId, merged)
}

Prevention

When it happens

Trigger: Calling the org member assign MCP tool with a primaryDeptId that is neither among the user's existing deptIds nor in the deptIDs being added, while primaryDeptId != 0.

Common situations: Typo or stale department ID from a deleted dept; passing a parent/ancestor department expecting implicit membership; forgetting to include the primary dept in the deptIDs list of the same call.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/4aa1580426398494. Report an issue: GitHub.

Appendix: source

Thrown at server/mcp/org_member_assigner.go:155

		UserID:   user.ID,
		UserName: user.Username,
	}
	var messages []string

	if len(deptIDs) > 0 {
		merged, added := mergeUintSets(currentDeptIDs, deptIDs)

		// 主部门语义:显式传参优先,否则保留用户现有主部门;上游对 0 值取集合首个
		primary := user.DeptId
		if value, ok := args["primaryDeptId"]; ok && value != nil {
			explicit, err := parseUintParam(value, "primaryDeptId")
			if err != nil {
				return nil, err
			}
			primary = explicit
		}
		if primary != 0 && !containsUint(merged, primary) {
			return nil, fmt.Errorf("primaryDeptId %d 必须属于合并后的部门集合 %v", primary, merged)
		}

		if len(added) > 0 || (primary != user.DeptId && primary != 0) {
			if _, err := postUpstream[map[string]any](ctx, "/user/setUserDepartments", map[string]any{
				"ID":            user.ID,
				"deptIds":       merged,
				"primaryDeptId": primary,
			}); err != nil {
				return nil, fmt.Errorf("设置用户部门失败: %w", err)
			}
			messages = append(messages, fmt.Sprintf("部门新增 %v", added))
		} else {
			messages = append(messages, "部门均已存在,未写入")
		}
		result.Departments = &orgAssignChange{Before: currentDeptIDs, After: merged, Added: added}
		// 上游对 primaryDeptId=0 取部门集合首个;如实回报生效主部门,不因 0 值被 omitempty 隐去而误导
		result.PrimaryDeptID = primary
		if primary == 0 && len(merged) > 0 {

View on GitHub (pinned to 3136500ef3)