dgraph-io/dgraph · error

Cannot index attribute %s of type object.

Error message

Cannot index attribute %s of type object.

What it means

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.

Source

Thrown at posting/index.go:58

var emptyCountParams countParams

type indexMutationInfo struct {
	tokenizers   []tok.Tokenizer
	factorySpecs []*tok.FactoryCreateSpec
	edge         *pb.DirectedEdge // Represents the original uid -> value edge.
	val          types.Val
	op           pb.DirectedEdge_Op
}

// indexTokens return tokens, without the predicate prefix and
// index rune, for specific tokenizers.
func indexTokens(ctx context.Context, info *indexMutationInfo) ([]string, error) {
	attr := info.edge.Attr
	lang := info.edge.GetLang()

	schemaType, err := schema.State().TypeOf(attr)
	if err != nil || !schemaType.IsScalar() {
		return nil, errors.Errorf("Cannot index attribute %s of type object.", attr)
	}

	if !schema.State().IsIndexed(ctx, attr) {
		return nil, errors.Errorf("Attribute %s is not indexed.", attr)
	}
	sv, err := types.Convert(info.val, schemaType)
	if err != nil {
		return nil, err
	}

	var tokens []string
	for _, it := range info.tokenizers {
		toks, err := tok.BuildTokens(sv.Value, tok.GetTokenizerForLang(it, lang))
		if err != nil {
			return tokens, err
		}
		tokens = append(tokens, toks...)
	}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Check the predicate's type in the schema and only index scalar predicates (string, int, float, bool, datetime, geo)
  2. Apply a schema entry for the attribute before mutating (missing schema causes TypeOf to error)
  3. If you intended to index a UID edge, remove the index expectation — UID edges are not token-indexed
  4. Re-run schema update: add the predicate with the correct scalar type and @index directive, then retry the mutation

Example fix

// before: edge.Attr = "friend" which is [uid] in schema -> indexTokens fails
// after: fix schema so the attribute is scalar and indexed, e.g. DQL schema:
// name: string @index(hash) .
edge := &pb.DirectedEdge{Attr: "name", Value: []byte("Alice"), ValueType: pb.Posting_STRING}
err := AddMutationWithIndex(ctx, edge, nil)
Defensive patterns

Strategy: validation

Validate before calling

// Check predicate type before attempting indexed mutation
sch, err := client.Schema(context.Background(), pred) // or query /schema
if err != nil || sch.Type != "string" { // must be scalar, not uid
    return fmt.Errorf("predicate %s is not a scalar type; cannot index", pred)
}

Type guard

func isScalarPredicate(t dgraphSchemaType) bool {
    switch t.Type {
    case "string", "int", "float", "bool", "datetime", "geo":
        return true
    }
    return false
}

Try / catch

err := addIndexedMutation(edge)
if err != nil && strings.Contains(err.Error(), "Cannot index attribute") {
    return fmt.Errorf("predicate %q is a uid/object type; remove it from the indexing path", edge.Attr)
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/c305af0335e90aca. Report an issue: GitHub.