dgraph-io/dgraph · error

ObjectId should be > 0 for nquad: %+v

Error message

ObjectId should be > 0 for nquad: %+v

What it means

In ToEdgeUsing, when the N-Quad's object is a UID-valued edge (x.ValueUid), the object is resolved with toUid; if the resulting object UID is 0 the edge would point at nothing, so the library returns this error. Only UID-typed objects take this path; literal-valued objects go through CreateValueEdge and are validated elsewhere.

Source

Thrown at dql/mutation.go:188

func (nq NQuad) ToEdgeUsing(newToUid map[string]uint64) (*pb.DirectedEdge, error) {
	var edge *pb.DirectedEdge
	sUid, err := toUid(nq.Subject, newToUid)
	if err != nil {
		return nil, err
	}

	if sUid == 0 {
		return nil, errors.Errorf("Subject should be > 0 for nquad: %+v", nq)
	}

	switch nq.valueType() {
	case x.ValueUid:
		oUid, err := toUid(nq.ObjectId, newToUid)
		if err != nil {
			return nil, err
		}
		if oUid == 0 {
			return nil, errors.Errorf("ObjectId should be > 0 for nquad: %+v", nq)
		}
		edge = nq.CreateUidEdge(sUid, oUid)
	case x.ValuePlain, x.ValueMulti:
		edge, err = nq.CreateValueEdge(sUid)
	default:
		return &emptyEdge, errors.Errorf("Unknown value type for nquad: %+v", nq)
	}
	if err != nil {
		return nil, err
	}
	return edge, nil
}

func copyValue(out *pb.DirectedEdge, nq NQuad) error {
	var err error
	var t types.TypeID
	if out.Value, t, err = byteVal(nq); err != nil {
		return err

View on GitHub (pinned to 759e242be6)

Solutions

  1. Verify the object blank-node label matches exactly (including case) the label used where the node is created in newToUid
  2. Pre-assign UIDs for every referenced node (e.g. via upsert blocks or a first mutation pass) before building edges to them
  3. Confirm explicit object UIDs are non-zero and correspond to existing nodes
  4. Inspect the nquad in the error message to identify the failing object value

Example fix

// before
toUid := map[string]uint64{"_:alice": 0x1001} // _:bob missing
// after
toUid := map[string]uint64{"_:alice": 0x1001, "_:bob": 0x1002}
Defensive patterns

Strategy: validation

Validate before calling

func validObject(nq *api.NQuad, toUid map[string]uint64) bool {
  if nq.ObjectId == "" { return nq.ObjectValue != nil }
  if uid, err := strconv.ParseUint(nq.ObjectId, 0, 64); err == nil { return uid != 0 }
  if strings.HasPrefix(nq.ObjectId, "_:") { return toUid[nq.ObjectId] != 0 }
  return false
}

Prevention

When it happens

Trigger: Calling ToEdgeUsing with an N-Quad whose object is a UID reference (another node or blank node) that resolves to 0 — e.g. a blank node _:bob absent from the newToUid map, or an explicit object UID of 0.

Common situations: Typos in blank-node labels between subject and object positions of quads; mutations referencing nodes that were never upserted/allocated; copying example code that uses UIDs that don't exist in your graph.

Related errors


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