dgraph-io/dgraph · error

error while parsing [%v]

Error message

error while parsing [%v]

What it means

This error wraps a failure from parseSubject() while Dgraph processes a mutation against predicates that carry a @unique constraint. During verifyUnique, every N-Quad subject must resolve to a UID: either a uid(var) reference or a literal UID like '0x123'. If the subject string is neither, parseSubject fails and this wrapper adds the offending subject to the message. It indicates a malformed mutation edge, not a uniqueness problem.

Source

Thrown at edgraph/server.go:1806

		isEmpty := func(l *pb.List) bool {
			return l == nil || len(l.Uids) == 0
		}

		var subjectUid uint64
		if strings.HasPrefix(pred.Subject, "uid(") {
			varName := qr.Vars[pred.Subject[4:len(pred.Subject)-1]]
			if isEmpty(varName.Uids) {
				subjectUid = 0 // blank node
			} else if len(varName.Uids.Uids) == 1 {
				subjectUid = varName.Uids.Uids[0]
			} else {
				return errors.Errorf("unique constraint violated for predicate [%v]", pred.Predicate)
			}
		} else {
			var err error
			subjectUid, err = parseSubject(pred.Subject)
			if err != nil {
				return errors.Wrapf(err, "error while parsing [%v]", pred.Subject)
			}
		}

		var predValue interface{}
		if strings.HasPrefix(pred.ObjectId, "val(") {
			varName := qr.Vars[pred.ObjectId[4:len(pred.ObjectId)-1]]
			val, ok := varName.Vals.Get(0)
			if !ok {
				_, isValueGoingtoSet := varName.Vals.Get(subjectUid)
				if !isValueGoingtoSet {
					continue
				}

				results := qr.Vars[queryVar.valVar]
				err := results.Vals.Iterate(func(uidOfv uint64, v types.Val) error {
					varNameVal, _ := varName.Vals.Get(subjectUid)
					if v.Value == varNameVal.Value && uidOfv != subjectUid {
						return errors.Errorf("could not insert duplicate value [%v] for predicate [%v]",

View on GitHub (pinned to 759e242be6)

Solutions

  1. Inspect the wrapped message for the offending subject value and fix it in the mutation payload to a valid UID (0x...), blank node (_:name), or uid(var) reference
  2. If you intended to match by value, run an upsert query first (upsert block with 'uid(var)' or 'uid(...)' in the mutation) instead of putting the raw value in the subject position
  3. If the value comes from client input, validate it server-side with a regex like ^0x[0-9a-f]+$ before building the N-Quad
  4. Check SDK serialization: ensure you pass Dgraph.UID objects, not arbitrary strings, when constructing mutations programmatically

Example fix

// before
{ "set": [ { "uid": "alice@example.com", "name": "Alice" } ] }
// after (upsert by value first)
upsert(query: "{ a as var(func: eq(email, \"alice@example.com\")) }", mutation: "{ set { uid(a) <name> \"Alice\" . } }")
Defensive patterns

Strategy: validation

Validate before calling

import "regexp"
var uidRe = regexp.MustCompile(`^(0x[0-9a-fA-F]+|_:[A-Za-z_][A-Za-z0-9_.-]*|uid\([^)]+\))$`)
func validSubject(s string) bool { return uidRe.MatchString(s) }
// reject or upsert-by-value when validSubject(nquad.Subject) is false

Type guard

func isUIDRef(s string) bool {
	return strings.HasPrefix(s, "0x") && len(s) >= 3
}

Try / catch

err := dgoMutate(ctx, mu)
if err != nil && strings.Contains(err.Error(), "error while parsing") {
	// malformed subject: fall back to upsert-by-value flow
}

Prevention

When it happens

Trigger: Sending a Mutation with an N-Quad whose subject is not a valid UID (0x-hex), not a blank node (_:name), and not a uid(var) reference — e.g. subject set to a plain string, an empty value, or a numeric ID — while the mutation runs through the unique-constraint upsert path (verifyUnique at edgraph/server.go:1806).

Common situations: Hand-built RDF strings with typos; JSON mutations where the 'uid' field is sent as a name/email string instead of '0x...' (developers expect upsert-by-value, which only happens via @upsert + query); client SDKs serializing objects whose uid was never assigned; referencing a GraphQL id field instead of the Dgraph UID.

Related errors


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