{"record":{"id":"0878cb2cf7a193cb","repo":"dgraph-io/dgraph","slug":"uid-must-to-be-greater-than-0","errorCode":null,"errorMessage":"UID must to be greater than 0","messagePattern":"UID must to be greater than 0","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dql/mutation.go","lineNumber":20,"sourceCode":" * SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.\n * SPDX-License-Identifier: Apache-2.0\n */\n\npackage dql\n\nimport (\n\t\"strconv\"\n\n\t\"github.com/pkg/errors\"\n\n\t\"github.com/dgraph-io/dgo/v250/protos/api\"\n\t\"github.com/dgraph-io/dgraph/v25/protos/pb\"\n\t\"github.com/dgraph-io/dgraph/v25/types\"\n\t\"github.com/dgraph-io/dgraph/v25/x\"\n)\n\nvar (\n\terrInvalidUID = errors.New(\"UID must to be greater than 0\")\n)\n\n// Mutation stores the strings corresponding to set and delete operations.\ntype Mutation struct {\n\tCond         string\n\tSet          []*api.NQuad\n\tDel          []*api.NQuad\n\tAllowedPreds []string\n\n\tMetadata *pb.Metadata\n}\n\n// ParseUid parses the given string into an UID. This method returns with an error\n// if the string cannot be parsed or the parsed UID is zero.\nfunc ParseUid(xid string) (uint64, error) {\n\t// If string represents a UID, convert to uint64 and return.\n\tuid, err := strconv.ParseUint(xid, 0, 64)\n\tif err != nil {","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/dql/mutation.go#L2-L38","documentation":"errInvalidUID is declared in dql/mutation.go and returned by ParseUid and toUid when a UID value is zero or invalid. UIDs in Dgraph must be strictly greater than 0; 0 is reserved as the empty/invalid node id. The library throws it to prevent constructing edges or mutations against a non-existent node.","triggerScenarios":"Calling ParseUid with a string that parses to 0 (e.g. \"0x0\", \"0\") or toUid receiving a generated/looked-up UID of 0 during mutation processing (`UID must to be greater than 0`).","commonSituations":"Parsing user-supplied hex UIDs where \"0x0\" slipped through client validation, an upsert/xid lookup that returned no row and defaulted to 0, or a client that initializes its UID counter at 0 instead of 1.","solutions":["Validate UIDs before calling mutation APIs: parse and reject any uid <= 0 (or \"0x0\" strings) in client code.","Ensure xid-to-UID lookups check for 'not found' and return an explicit error rather than a zero value UID.","If assigning UIDs yourself, start from 1 (Dgraph allocates leases starting at 1).","Use ParseUid's error return to surface a user-facing message instead of letting 0 reach the edge construction."],"exampleFix":"// before\nuid := uint64(0)\nedge := &pb.DirectedEdge{Entity: uid} // invalid\n// after\nif uid == 0 {\n    return errors.New(\"valid UID required; got 0\")\n}\nedge := &pb.DirectedEdge{Entity: uid}","handlingStrategy":"validation","validationCode":"func validUid(u uint64) bool { return u > 0 }\nfunc validUidStr(s string) bool {\n    n, err := strconv.ParseUint(strings.TrimPrefix(s, \"0x\"), 0, 64)\n    return err == nil && n > 0\n}","typeGuard":"func isUID(v uint64) bool { return v > 0 }\n// usage: if !isUID(uid) { return ErrInvalidUID }","tryCatchPattern":"uid, err := dql.ParseUid(s)\nif err != nil {\n    if errors.Is(err, dql.ErrInvalidUID) || strings.Contains(err.Error(), \"greater than 0\") {\n        return fmt.Errorf(\"rejecting zero/invalid UID input %q\", s)\n    }\n    return err\n}","preventionTips":["Reject UID 0 / \"0x0\" at the API boundary before building mutations.","Treat failed xid lookups as errors, not zero UIDs.","Remember Dgraph UIDs start at 1; never initialize counters at 0."],"tags":["dgraph","uid","mutation"],"backgroundTag":"invalid-uid","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}