flipped-aurora/gin-vue-admin · error

模块 %d 字段 %d 的 dataSource.association 必须是 1 或 2

Error message

模块 %d 字段 %d 的 dataSource.association 必须是 1 或 2

What it means

When a field has a dataSource block, its association value must be 1 (dictionary/direct options) or 2 (associated table). Any other integer (0, 3, -1) fails validation. Note association=2 is auto-corrected to force fieldType=array before this check.

Source

Thrown at server/mcp/gva_execute.go:392

				if !slices.Contains(validFieldTypes, field.FieldType) {
					return fmt.Errorf("模块 %d 字段 %d 的 fieldType '%s' 不支持", moduleIndex+1, i+1, field.FieldType)
				}

				if field.FieldSearchType != "" {
					validSearchTypes := []string{"=", "!=", ">", ">=", "<", "<=", "LIKE", "BETWEEN", "NOT BETWEEN", "IN", "NOT IN"}
					if !slices.Contains(validSearchTypes, field.FieldSearchType) {
						return fmt.Errorf("模块 %d 字段 %d 的 fieldSearchType '%s' 不支持", moduleIndex+1, i+1, field.FieldSearchType)
					}
				}

				if field.DataSource != nil {
					associationValue := field.DataSource.Association
					if associationValue == 2 && field.FieldType != "array" {
						logger.WithCtx(ctx).Mod("mcp").Info(fmt.Sprintf("module %d field %d association=2, force fieldType to array", moduleIndex+1, i+1))
						moduleInfo.Fields[i].FieldType = "array"
					}
					if associationValue != 1 && associationValue != 2 {
						return fmt.Errorf("模块 %d 字段 %d 的 dataSource.association 必须是 1 或 2", moduleIndex+1, i+1)
					}
				}
			}

			if !moduleInfo.GvaModel {
				primaryKeyCount := 0
				for _, field := range moduleInfo.Fields {
					if field.PrimaryKey {
						primaryKeyCount++
					}
				}
				if primaryKeyCount == 0 {
					return fmt.Errorf("模块 %d:当 gvaModel=false 时,必须有一个字段的 primaryKey=true", moduleIndex+1)
				}
				if primaryKeyCount > 1 {
					return fmt.Errorf("模块 %d:当 gvaModel=false 时,只能有一个字段的 primaryKey=true", moduleIndex+1)
				}
			} else {

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Set dataSource.association to 1 (dictionary options) or 2 (table association)
  2. Remove the dataSource block if the field has no data source
  3. Remember association=2 requires/forces fieldType=array; set fieldType=array explicitly for table associations

Example fix

// before
{"fieldJson":"category","fieldType":"int","columnName":"category","dataSource":{"association":3}}
// after
{"fieldJson":"category","fieldType":"array","columnName":"category","dataSource":{"association":2}}
Defensive patterns

Strategy: validation

Validate before calling

if f.DataSource != nil && f.DataSource.Association != 1 && f.DataSource.Association != 2 {
  return fmt.Errorf("association must be 1 or 2")
}

Type guard

func hasValidAssociation(f Field) bool {
  return f.DataSource == nil || f.DataSource.Association == 1 || f.DataSource.Association == 2
}

Prevention

When it happens

Trigger: dataSource present in the plan field but association omitted (zero value 0) or set to an invalid number.

Common situations: JSON serialization dropping an unset association; LLM inventing association codes like 3 for 'many-to-many'; copy of a template where association was a different enum.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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