{"record":{"id":"11b1cb622e3923ad","repo":"flipped-aurora/gin-vue-admin","slug":"d-gvamodel-false-primarykey-true","errorCode":null,"errorMessage":"模块 %d：当 gvaModel=false 时，必须有一个字段的 primaryKey=true","messagePattern":"模块 (.+?)：当 gvaModel=false 时，必须有一个字段的 primaryKey=true","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/mcp/gva_execute.go","lineNumber":405,"sourceCode":"\t\t\t\t\tif associationValue == 2 && field.FieldType != \"array\" {\n\t\t\t\t\t\tlogger.WithCtx(ctx).Mod(\"mcp\").Info(fmt.Sprintf(\"module %d field %d association=2, force fieldType to array\", moduleIndex+1, i+1))\n\t\t\t\t\t\tmoduleInfo.Fields[i].FieldType = \"array\"\n\t\t\t\t\t}\n\t\t\t\t\tif associationValue != 1 && associationValue != 2 {\n\t\t\t\t\t\treturn fmt.Errorf(\"模块 %d 字段 %d 的 dataSource.association 必须是 1 或 2\", moduleIndex+1, i+1)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif !moduleInfo.GvaModel {\n\t\t\t\tprimaryKeyCount := 0\n\t\t\t\tfor _, field := range moduleInfo.Fields {\n\t\t\t\t\tif field.PrimaryKey {\n\t\t\t\t\t\tprimaryKeyCount++\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif primaryKeyCount == 0 {\n\t\t\t\t\treturn fmt.Errorf(\"模块 %d：当 gvaModel=false 时，必须有一个字段的 primaryKey=true\", moduleIndex+1)\n\t\t\t\t}\n\t\t\t\tif primaryKeyCount > 1 {\n\t\t\t\t\treturn fmt.Errorf(\"模块 %d：当 gvaModel=false 时，只能有一个字段的 primaryKey=true\", moduleIndex+1)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfor i, field := range moduleInfo.Fields {\n\t\t\t\t\tif field.PrimaryKey {\n\t\t\t\t\t\treturn fmt.Errorf(\"模块 %d：当 gvaModel=true 时，字段 %d 的 primaryKey 应该为 false\", moduleIndex+1, i+1)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// executeCreation 执行创建操作","sourceCodeStart":387,"sourceCodeEnd":423,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/mcp/gva_execute.go#L387-L423","documentation":"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.","triggerScenarios":"Module entry with gvaModel=false and all fields having primaryKey=false or omitted.","commonSituations":"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.","solutions":["Add \"primaryKey\":true to the intended ID field (e.g. the auto-increment id column)","Or set gvaModel=true if the standard GVA base model with implicit ID is what you want","Ensure exactly one field has primaryKey=true (see also the companion >1 error)"],"exampleFix":"// before\n{\"moduleName\":\"order\",\"gvaModel\":false,\"fields\":[{\"fieldJson\":\"id\",\"fieldType\":\"int\",\"columnName\":\"id\",\"primaryKey\":false}]}\n// after\n{\"moduleName\":\"order\",\"gvaModel\":false,\"fields\":[{\"fieldJson\":\"id\",\"fieldType\":\"int\",\"columnName\":\"id\",\"primaryKey\":true}]}","handlingStrategy":"validation","validationCode":"if !m.GvaModel {\n  pk := 0\n  for _, f := range m.Fields { if f.PrimaryKey { pk++ } }\n  if pk == 0 { return fmt.Errorf(\"module %s needs one primaryKey=true field\", m.Name) }\n}","typeGuard":"func hasPrimaryKey(m Module) bool {\n  for _, f := range m.Fields { if f.PrimaryKey { return true } }\n  return false\n}","tryCatchPattern":null,"preventionTips":["For gvaModel=false always designate one ID field with primaryKey=true","Use gvaModel=true when the standard GVA base model suffices","Count primaryKey flags in a pre-submit lint"],"tags":["validation","primary-key","code-generator"],"backgroundTag":"missing-primary-key","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}