dgraph-io/dgraph · error

Subject should be > 0 for nquad: %+v

Error message

Subject should be > 0 for nquad: %+v

What it means

ToEdgeUsing converts a parsed N-Quad into a Dgraph edge, resolving the subject node to a UID via toUid (using newToUid for blank-node mapping). If the resolved subject UID is 0, there is no valid node to anchor the edge, so the library refuses to build it. UID 0 is reserved/invalid in Dgraph, and an edge with a zero subject would be silently corrupt, so this is an explicit fail-fast check.

Source

Thrown at dql/mutation.go:178

	}

	if err := copyValue(out, nq); err != nil {
		return &emptyEdge, err
	}
	return out, nil
}

// ToEdgeUsing determines the UIDs for the provided XIDs and populates the
// xidToUid map.
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 {

View on GitHub (pinned to 759e242be6)

Solutions

  1. Check the N-Quad's subject string for typos and ensure blank-node labels (_:name) are consistent across all quads in the mutation
  2. Ensure the newToUid map passed to ToEdgeUsing assigns a non-zero UID for every blank node referenced as a subject
  3. If using explicit UIDs, verify the subject UID is a real, previously-assigned UID (never 0)
  4. Log/print the full nquad (%+v) included in the error to see exactly which subject failed resolution

Example fix

// before
mu, _ := dql.MakeMutationEdge(nq, nil) // _:alice missing from map
// after
toUid := map[string]uint64{"_:alice": 0x1001}
edge, err := ToEdgeUsing(nq, toUid)
if err != nil { return fmt.Errorf("quad %v: %w", nq, err) }
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Calling ToEdgeUsing (directly or via mutation building from dql.ParseMutation/Parse) with an N-Quad whose subject is a blank node like _:alice that is not present in (and cannot be assigned from) the newToUid map, or whose subject resolves to UID 0.

Common situations: Building mutations from raw RDF strings where a blank-node label is misspelled or inconsistently reused across quads; reusing a toUid map where the entry was never populated; hand-constructed api.NQuad objects with empty/zero subject UIDs.

Related errors


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