dgraph-io/dgraph · error

UID not found/generated for xid %s

Error message

UID not found/generated for xid %s

What it means

toUid resolves a mutation subject to a UID: it first checks numeric UIDs, then a newToUid map of xids that were assigned UIDs earlier in the same mutation. This error is thrown when the subject is an xid that was neither a known UID nor previously mapped in this mutation, so no UID exists or was generated for it (`UID not found/generated for xid %s`).

Source

Thrown at dql/mutation.go:109

		return p.Value.([]byte), p.Tid, nil
	}

	p1 := types.ValueForType(types.BinaryID)
	if err := types.Marshal(p, &p1); err != nil {
		return []byte{}, p.Tid, err
	}
	return p1.Value.([]byte), p.Tid, nil
}

func toUid(subject string, newToUid map[string]uint64) (uid uint64, err error) {
	if id, err := ParseUid(subject); err == nil || err == errInvalidUID {
		return id, err
	}
	// It's an xid
	if id, present := newToUid[subject]; present {
		return id, err
	}
	return 0, errors.Errorf("UID not found/generated for xid %s\n", subject)
}

var emptyEdge pb.DirectedEdge

func (nq NQuad) createEdgePrototype(subjectUid uint64) *pb.DirectedEdge {
	return &pb.DirectedEdge{
		Entity:    subjectUid,
		Attr:      nq.Predicate,
		Namespace: nq.Namespace,
		Lang:      nq.Lang,
		Facets:    nq.Facets,
	}
}

// CreateUidEdge returns a Directed edge connecting the given subject and object UIDs.
func (nq NQuad) CreateUidEdge(subjectUid uint64, objectUid uint64) *pb.DirectedEdge {
	out := nq.createEdgePrototype(subjectUid)
	out.ValueId = objectUid

View on GitHub (pinned to 759e242be6)

Solutions

  1. Include the xid-declaration N-Quad in the same mutation: `<*> <xid_name> "alice" .` so the parser generates/registers a UID for the xid.
  2. Reference the node by its numeric UID (e.g. `<0x123>`) instead of the raw xid if the UID is already known.
  3. Ensure the xid predicate name in the N-Quad exactly matches the configured/schematized xid predicate.
  4. If data was loaded earlier, look up the existing UID first and embed it in subsequent mutations.

Example fix

// before
{"set":[{"subject":"alice","predicate":"name","objectValue":"Alice"}]} // xid never declared
// after
{"set":[{"subject":"*","predicate":"xid","objectValue":"alice"},{"subject":"alice","predicate":"name","objectValue":"Alice"}]}
Defensive patterns

Strategy: validation

Validate before calling

func checkXidDeclared(mutations []NQuad, xidPred, xid string) bool {
    for _, nq := range mutations {
        if nq.Predicate == xidPred && nq.ObjectValue.GetStringValue() == xid {
            return true // <*> <xidPred> "xid" generates a UID
        }
    }
    return false
}

Try / catch

id, err := toUidWrapper(subject)
if err != nil && strings.Contains(err.Error(), "UID not found/generated for xid") {
    return fmt.Errorf("subject %q is an xid; add '<*> <xid> %q' to the mutation or use a numeric UID", subject, subject)
}

Prevention

When it happens

Trigger: Submitting an N-Quad whose subject is an external id (xid, e.g. `"alice" <name> "Alice" .`) without the `<*> <xid> "alice"` edge that tells Dgraph to generate a UID for that xid in the same mutation.

Common situations: Bulk-loading data where the xid-declaration edge (`* <xid> *`) was omitted or misspelled, referencing an xid in a later mutation that was only created in an earlier (separate) request, or xid predicate configured differently between runs.

Related errors


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