dgraph-io/dgraph · error

Invalid UID with value 0 for key: %v

Error message

Invalid UID with value 0 for key: %v

What it means

Parse() in x/keys.go decodes Dgraph's binary key layout. For ByteData/ByteReverse keys the payload must contain a non-zero 8-byte big-endian UID; a zero UID means a malformed or corrupted key, so Parse refuses to return it.

Source

Thrown at x/keys.go:573

	k = k[sz:]

	switch p.bytePrefix {
	case ByteSchema, ByteType:
		return p, nil
	default:
	}

	p.ByteType = k[0]
	k = k[1:]

	switch p.ByteType {
	case ByteData, ByteReverse:
		if len(k) < 8 {
			return p, errors.Errorf("uid length < 8 for key: %q, parsed key: %+v", key, p)
		}
		p.Uid = binary.BigEndian.Uint64(k)
		if p.Uid == 0 {
			return p, errors.Errorf("Invalid UID with value 0 for key: %v", key)
		}
		if !p.HasStartUid {
			break
		}

		if len(k) != 16 {
			return p, errors.Errorf("StartUid length != 8 for key: %q, parsed key: %+v", key, p)
		}

		k = k[8:]
		p.StartUid = binary.BigEndian.Uint64(k)
	case ByteIndex:
		if !p.HasStartUid {
			p.Term = string(k)
			break
		}

		if len(k) < 8 {

View on GitHub (pinned to 759e242be6)

Solutions

  1. Dump the key with hex.Dump and verify the first 8 bytes are the intended non-zero UID
  2. Regenerate the key through the proper Key/Schema encoding helpers instead of constructing it manually
  3. Check for writers producing zero UIDs (uninitialized Uid fields) in your data path
  4. If this arises during a drop op, inspect the posting list for corruption and consider restoring from backup

Example fix

// before: hand-built data key with zero UID
key := append([]byte{0x00}, make([]byte, 9)...)
ok, err := x.IsDropOpKey(key) // errors: Invalid UID with value 0
// after: build keys via the library
key := x.IndexKey(attr, uid) // ensures valid non-zero UID
Defensive patterns

Strategy: validation

Validate before calling

func validDataKey(key []byte) bool {
  if len(key) < 9 { return false }
  uid := binary.BigEndian.Uint64(key[1:9])
  return uid != 0
}
if !validDataKey(key) { return fmt.Errorf("bad key: %x", key) }
ok, err := x.IsDropOpKey(key)

Type guard

func hasNonZeroUid(key []byte) bool {
  if len(key) < 9 { return false }
  return binary.BigEndian.Uint64(key[1:9]) != 0
}

Prevention

When it happens

Trigger: Calling Parse (directly or via IsDropOpKey, e.g. during a drop operation in checkAndGetDropOp) with a data-type key whose length is >= 8 but whose first 8 bytes decode to uint64(0).

Common situations: Corrupted badger key data, hand-crafted keys passed to IsDropOpKey, keys written by buggy or older Dgraph versions with unset UIDs, or test fixtures with zero-filled data keys.

Related errors


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