{"record":{"id":"639d4149422f2bc8","repo":"dgraph-io/dgraph","slug":"unique-directive-not-supported-on-v-type-predi","errorCode":null,"errorMessage":"@unique directive not supported on [%v] type predicate","messagePattern":"@unique directive not supported on \\[(.+?)\\] type predicate","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"worker/mutation.go","lineNumber":502,"sourceCode":"\t\tif len(currentSchema.Tokenizer) == 0 ||\n\t\t\t(len(currentSchema.Tokenizer) > 0 && !validTokenizer(currentSchema.Tokenizer)) {\n\n\t\t\treturn errors.Errorf(\"index for predicate [%v] is missing, add either hash or exact index with @unique\",\n\t\t\t\tx.ParseAttr(currentSchema.Predicate))\n\t\t} else {\n\t\t\treturn nil\n\t\t}\n\n\tcase pb.Posting_INT:\n\t\tif len(currentSchema.Tokenizer) == 0 {\n\t\t\treturn errors.Errorf(\"index for predicate [%v] is missing, add int index with @unique\",\n\t\t\t\tx.ParseAttr(currentSchema.Predicate))\n\t\t} else {\n\t\t\treturn nil\n\t\t}\n\t}\n\n\treturn errors.Errorf(\"@unique directive not supported on [%v] type predicate\", currentSchema.ValueType.String())\n}\n\n// ValidateAndConvert checks compatibility or converts to the schema type if the storage type is\n// specified. If no storage type is specified then it converts to the schema type.\nfunc ValidateAndConvert(edge *pb.DirectedEdge, su *pb.SchemaUpdate) error {\n\n\tif isDeletePredicateEdge(edge) {\n\t\treturn nil\n\t}\n\tif types.TypeID(edge.ValueType) == types.DefaultID && isStarAll(edge.Value) {\n\t\treturn nil\n\t}\n\n\tif strings.Contains(su.Predicate, hnsw.VecKeyword) {\n\t\treturn errors.Errorf(\"Not allowed to insert mutations in vector index keys, edge: [%v]\", edge)\n\t}\n\n\tstorageType := posting.TypeID(edge)","sourceCodeStart":484,"sourceCodeEnd":520,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/worker/mutation.go#L484-L520","documentation":"The @unique directive is only meaningful on string and int predicates. If the predicate's type is anything else (float, bool, datetime, uid, etc.), validateSchemaForUnique falls through the type switch and rejects the schema because Dgraph has no tokenizer/index mechanism to enforce uniqueness for that value type.","triggerScenarios":"Submitting an alter that puts @unique on a predicate whose schema type is not string or int, e.g. `createdAt datetime @index(hour) @unique .` or `active bool @unique .` Raised by checkSchema -> validateSchemaForUnique.","commonSituations":"Porting SQL UNIQUE constraints to Dgraph for date/bool/float columns; assuming @unique works on any scalar; typos in the predicate type declaration.","solutions":["Remove @unique from the non-string/non-int predicate.","Move uniqueness enforcement to the application layer (check-then-set inside a transaction) for other types.","If a string or int representation is acceptable, change the predicate type so a hash/exact/int index can back @unique."],"exampleFix":"// before (fails)\ncreatedAt datetime @index(hour) @unique .\n\n// after\ncreatedAt datetime @index(hour) .\n// enforce uniqueness in app logic or via a unique string/int surrogate key","handlingStrategy":"validation","validationCode":"UNIQUE_OK_TYPES = {'string', 'int'}\nfor pred in schemaChanges:\n    if pred.unique and pred.type not in UNIQUE_OK_TYPES:\n        raise ValueError(f\"@unique not supported on {pred.type} predicate {pred.name}\")","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"@unique directive not supported\") {\n    // move uniqueness enforcement to application logic for this type\n}","preventionTips":["Only use @unique on string and int predicates.","For other scalar types, enforce uniqueness with an upsert (query-then-set) transaction.","Document which predicates carry uniqueness invariants in the schema file."],"tags":["dgraph","schema","unique-constraint"],"backgroundTag":"unsupported-schema-directive","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}