flipped-aurora/gin-vue-admin · error

模块 %d:当 gvaModel=false 时,必须有一个字段的 primaryKey=true

Error message

模块 %d:当 gvaModel=false 时,必须有一个字段的 primaryKey=true

What it means

When a module declares gvaModel=false (custom model, not the standard gin-vue-admin base model with built-in ID), the plan must designate exactly one field as the primary key via primaryKey=true. This error fires when zero fields carry the primaryKey flag, leaving the generated table without a defined primary key.

Source

Thrown at server/mcp/gva_execute.go:405

					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 {
				for i, field := range moduleInfo.Fields {
					if field.PrimaryKey {
						return fmt.Errorf("模块 %d:当 gvaModel=true 时,字段 %d 的 primaryKey 应该为 false", moduleIndex+1, i+1)
					}
				}
			}
		}
	}

	return nil
}

// executeCreation 执行创建操作

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Add "primaryKey":true to the intended ID field (e.g. the auto-increment id column)
  2. Or set gvaModel=true if the standard GVA base model with implicit ID is what you want
  3. Ensure exactly one field has primaryKey=true (see also the companion >1 error)

Example fix

// before
{"moduleName":"order","gvaModel":false,"fields":[{"fieldJson":"id","fieldType":"int","columnName":"id","primaryKey":false}]}
// after
{"moduleName":"order","gvaModel":false,"fields":[{"fieldJson":"id","fieldType":"int","columnName":"id","primaryKey":true}]}
Defensive patterns

Strategy: validation

Validate before calling

if !m.GvaModel {
  pk := 0
  for _, f := range m.Fields { if f.PrimaryKey { pk++ } }
  if pk == 0 { return fmt.Errorf("module %s needs one primaryKey=true field", m.Name) }
}

Type guard

func hasPrimaryKey(m Module) bool {
  for _, f := range m.Fields { if f.PrimaryKey { return true } }
  return false
}

Prevention

When it happens

Trigger: Module entry with gvaModel=false and all fields having primaryKey=false or omitted.

Common situations: LLM forgets to mark an ID field as primaryKey when generating custom models; author assumed gvaModel=true behavior (implicit ID) applies; field objects copied from a gvaModel=true module where primaryKey flags are forbidden.

Related errors


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