{"record":{"id":"0aa2d39a16511978","repo":"dgraph-io/dgraph","slug":"index-not-allowed-on-predicate-of-type-uid-on-pred","errorCode":null,"errorMessage":"Index not allowed on predicate of type uid on predicate %s","messagePattern":"Index not allowed on predicate of type uid on predicate (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"worker/mutation.go","lineNumber":404,"sourceCode":"\t}\n\n\tif x.IsInternalPredicate(s.Predicate) {\n\t\treturn errors.Errorf(\"Cannot create user-defined predicate with internal name %s\",\n\t\t\tx.ParseAttr(s.Predicate))\n\t}\n\n\tif s.Directive == pb.SchemaUpdate_INDEX && !schema.HasTokenizerOrVectorIndexSpec(s) {\n\t\treturn errors.Errorf(\"Tokenizer must be specified while indexing a predicate: %+v\", s)\n\t}\n\n\tif schema.HasTokenizerOrVectorIndexSpec(s) && s.Directive != pb.SchemaUpdate_INDEX {\n\t\treturn errors.Errorf(\"Directive must be SchemaUpdate_INDEX when a tokenizer is specified\")\n\t}\n\n\ttyp := types.TypeID(s.ValueType)\n\tif typ == types.UidID && s.Directive == pb.SchemaUpdate_INDEX {\n\t\t// index on uid type\n\t\treturn errors.Errorf(\"Index not allowed on predicate of type uid on predicate %s\",\n\t\t\tx.ParseAttr(s.Predicate))\n\t} else if typ != types.UidID && s.Directive == pb.SchemaUpdate_REVERSE {\n\t\t// reverse on non-uid type\n\t\treturn errors.Errorf(\"Cannot reverse for non-uid type on predicate %s\",\n\t\t\tx.ParseAttr(s.Predicate))\n\t}\n\n\t// If schema update has upsert directive, it should have index directive.\n\tif s.Upsert && len(s.Tokenizer) == 0 && !s.Unique {\n\t\treturn errors.Errorf(\"Index tokenizer is mandatory for: [%s] when specifying @upsert directive\",\n\t\t\tx.ParseAttr(s.Predicate))\n\t}\n\n\tif s.Unique {\n\t\tctx := context.WithValue(context.Background(), schema.IsWrite, false)\n\t\tprevSchema, _ := schema.State().Get(ctx, s.Predicate)\n\t\tif err := validateSchemaForUnique(prevSchema, s); err != nil {\n\t\t\treturn err","sourceCodeStart":386,"sourceCodeEnd":422,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/worker/mutation.go#L386-L422","documentation":"Dgraph does not support indexing on predicates whose value type is uid, because uid edges are already stored in an indexed (postings-list) form. checkSchema rejects any update where ValueType is UidID and Directive is SchemaUpdate_INDEX, naming the offending predicate.","triggerScenarios":"Declaring or altering a uid predicate with an index directive, e.g. 'friend: [uid] @index(hash)' in a DQL schema, or constructing pb.SchemaUpdate with ValueType: pb.Posting_UID and Directive: pb.SchemaUpdate_INDEX.","commonSituations":"Copy-pasting '@index(term) or @index(hash)' across all predicates in a generated schema without excluding uid predicates; misunderstanding that @reverse suffices for uid edges; template-based schema tooling applying uniform directives.","solutions":["Remove the @index directive from the uid predicate: 'friend: [uid] @reverse'.","If fast lookup is needed on a uid edge, use @reverse instead of @index.","If the predicate should hold values other than uids, change its type to a scalar type (string, int, etc.) and keep the tokenizer.","Filter uid-typed predicates out of index-directive generation in schema tooling."],"exampleFix":"// before\nfriend: [uid] @index(term) .\n// after\nfriend: [uid] @reverse .","handlingStrategy":"validation","validationCode":"func validateNoIndexOnUid(s *pb.SchemaUpdate) error {\n    if types.TypeID(s.ValueType) == types.UidID && s.Directive == pb.SchemaUpdate_INDEX {\n        return fmt.Errorf(\"predicate %s: @index not allowed on uid\", s.Predicate)\n    }\n    return nil\n}","typeGuard":"func isUidPredicate(s *pb.SchemaUpdate) bool {\n    return types.TypeID(s.ValueType) == types.UidID\n}","tryCatchPattern":"err := dgraph.Alter(ctx, op)\nif err != nil && strings.Contains(err.Error(), \"Index not allowed on predicate of type uid\") {\n    // remove @index, use @reverse instead\n}","preventionTips":["Exclude uid predicates from index-directive generators","Use @reverse for reverse traversal on uid edges","Review auto-generated schema diffs for blanket directives"],"tags":["dgraph","schema","uid","indexing"],"backgroundTag":"schema-validation-failed","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}