{"record":{"id":"9cbba7f268268316","repo":"cayleygraph/cayley","slug":"required-field-is-missing","errorCode":null,"errorMessage":"required field is missing","messagePattern":"required field is missing","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"schema/loader.go","lineNumber":18,"sourceCode":"package schema\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"reflect\"\n\n\t\"github.com/cayleygraph/cayley/query/path\"\n\t\"github.com/cayleygraph/quad\"\n\n\t\"github.com/cayleygraph/cayley/graph\"\n\t\"github.com/cayleygraph/cayley/graph/iterator\"\n)\n\nvar (\n\terrNotFound               = errors.New(\"not found\")\n\terrRequiredFieldIsMissing = errors.New(\"required field is missing\")\n)\n\n// Optimize flags controls an optimization step performed before queries.\nvar Optimize = true\n\n// IsNotFound check if error is related to a missing object (either because of wrong ID or because of type constrains).\nfunc IsNotFound(err error) bool {\n\treturn err == errNotFound || err == errRequiredFieldIsMissing\n}\n\n// LoadTo will load a sub-graph of objects starting from ids (or from any nodes, if empty)\n// to a destination Go object. Destination can be a struct, slice or channel.\n//\n// Mapping to quads is done via Go struct tag \"quad\" or \"json\" as a fallback.\n//\n// A simplest mapping is an \"@id\" tag which saves node ID (subject of a quad) into tagged field.\n//\n//\ttype Node struct{","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/cayleygraph/cayley/blob/81dcd7d73e45136bc0d01802a8ba4685d8a533eb/schema/loader.go#L1-L36","documentation":"errRequiredFieldIsMissing is returned when loading or saving a mapped Go struct whose schema defines a required (non-optional) field, but the graph holds no value for it. It is treated as a not-found-class error (schema.IsNotFound returns true) because the object is effectively incomplete for the requested type.","triggerScenarios":"Loading a struct where a non-optional field (e.g. without schema:\"-,opt\" / not registered optional) has no quads in the graph (loadToValue/loadIteratorToDepth); saving rules in loadIteratorToDepth encountering an empty required field map.","commonSituations":"Partial writes that skipped a required property; entities created by another tool that doesn't set all fields; schema evolution adding a new required field to old data.","solutions":["Add the missing field value to the graph (re-save the entity with all required fields).","Mark the field optional in the schema tag so loading tolerates missing values.","Use schema.IsNotFound(err) to handle incomplete records gracefully instead of failing."],"exampleFix":"// before\ntype Person struct {\n    Name string `cayley:\"schema:name\"` // required, missing in DB\n}\n// after\ntype Person struct {\n    Name string `cayley:\"schema:name,opt\"` // or write the name quad\n}","handlingStrategy":"type-guard","validationCode":"// check required field has a value in the graph before load\nif len(valuesFor(qs, iri, schemaName)) == 0 {\n    return errors.New(\"required field 'name' missing for \" + iri)\n}","typeGuard":"func isIncompleteRecord(err error) bool {\n    return schema.IsNotFound(err) && err.Error() == \"required field is missing\"\n}","tryCatchPattern":"err := schema.LoadTo(ctx, qs, &obj, iri)\nif err != nil && err.Error() == \"required field is missing\" {\n    // handle partial record: backfill, default, or skip\n}","preventionTips":["Mark genuinely optional fields with the opt schema tag","Validate entities at write time so required fields are always stored","Backfill data after schema changes that add required fields"],"tags":["go","schema","missing-field","validation"],"backgroundTag":"missing-required-argument","analyzedSha":"81dcd7d73e45136bc0d01802a8ba4685d8a533eb","analyzedAt":"2026-09-06T06:14:12.358Z","contentChangedAt":"2026-09-06T06:14:12.358Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}