{"record":{"id":"c305af0335e90aca","repo":"dgraph-io/dgraph","slug":"cannot-index-attribute-s-of-type-object","errorCode":null,"errorMessage":"Cannot index attribute %s of type object.","messagePattern":"Cannot index attribute (.+?) of type object\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"posting/index.go","lineNumber":58,"sourceCode":"var emptyCountParams countParams\n\ntype indexMutationInfo struct {\n\ttokenizers   []tok.Tokenizer\n\tfactorySpecs []*tok.FactoryCreateSpec\n\tedge         *pb.DirectedEdge // Represents the original uid -> value edge.\n\tval          types.Val\n\top           pb.DirectedEdge_Op\n}\n\n// indexTokens return tokens, without the predicate prefix and\n// index rune, for specific tokenizers.\nfunc indexTokens(ctx context.Context, info *indexMutationInfo) ([]string, error) {\n\tattr := info.edge.Attr\n\tlang := info.edge.GetLang()\n\n\tschemaType, err := schema.State().TypeOf(attr)\n\tif err != nil || !schemaType.IsScalar() {\n\t\treturn nil, errors.Errorf(\"Cannot index attribute %s of type object.\", attr)\n\t}\n\n\tif !schema.State().IsIndexed(ctx, attr) {\n\t\treturn nil, errors.Errorf(\"Attribute %s is not indexed.\", attr)\n\t}\n\tsv, err := types.Convert(info.val, schemaType)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tvar tokens []string\n\tfor _, it := range info.tokenizers {\n\t\ttoks, err := tok.BuildTokens(sv.Value, tok.GetTokenizerForLang(it, lang))\n\t\tif err != nil {\n\t\t\treturn tokens, err\n\t\t}\n\t\ttokens = append(tokens, toks...)\n\t}","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/posting/index.go#L40-L76","documentation":"indexTokens builds index tokens for an indexed edge during a mutation. It first looks up the predicate's schema type; if the lookup fails or the type is not scalar (i.e. it is a UID/object type), indexing is impossible and this error is thrown. Object (UID) edges have no indexable value token — only scalar predicates can be indexed.","triggerScenarios":"Calling AddMutationWithIndex / addIndexMutations with a DirectedEdge whose Attr resolves to an object-type (UID) predicate in the schema, or whose attribute has no schema entry at all (TypeOf returns an error).","commonSituations":"Mutating a UID predicate (e.g. a ~friend edge) while an indexing pipeline expects scalar tokens; schema dropped or not yet applied so TypeOf errs; using @index in DQL on a uid predicate incorrectly.","solutions":["Check the predicate's type in the schema and only index scalar predicates (string, int, float, bool, datetime, geo)","Apply a schema entry for the attribute before mutating (missing schema causes TypeOf to error)","If you intended to index a UID edge, remove the index expectation — UID edges are not token-indexed","Re-run schema update: add the predicate with the correct scalar type and @index directive, then retry the mutation"],"exampleFix":"// before: edge.Attr = \"friend\" which is [uid] in schema -> indexTokens fails\n// after: fix schema so the attribute is scalar and indexed, e.g. DQL schema:\n// name: string @index(hash) .\nedge := &pb.DirectedEdge{Attr: \"name\", Value: []byte(\"Alice\"), ValueType: pb.Posting_STRING}\nerr := AddMutationWithIndex(ctx, edge, nil)","handlingStrategy":"validation","validationCode":"// Check predicate type before attempting indexed mutation\nsch, err := client.Schema(context.Background(), pred) // or query /schema\nif err != nil || sch.Type != \"string\" { // must be scalar, not uid\n    return fmt.Errorf(\"predicate %s is not a scalar type; cannot index\", pred)\n}","typeGuard":"func isScalarPredicate(t dgraphSchemaType) bool {\n    switch t.Type {\n    case \"string\", \"int\", \"float\", \"bool\", \"datetime\", \"geo\":\n        return true\n    }\n    return false\n}","tryCatchPattern":"err := addIndexedMutation(edge)\nif err != nil && strings.Contains(err.Error(), \"Cannot index attribute\") {\n    return fmt.Errorf(\"predicate %q is a uid/object type; remove it from the indexing path\", edge.Attr)\n}","preventionTips":["Apply schema before any data mutation","Only mark scalar predicates with @index; uid predicates cannot be token-indexed","Keep schema definitions in migration scripts applied to every environment","Verify the attribute exists in the schema (missing schema also triggers this error via TypeOf failure)"],"tags":["indexing","schema","uid-predicate","mutation"],"backgroundTag":"schema-type-mismatch","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}