dgraph-io/dgraph · error

Expected 1 but got %d facets

Error message

Expected 1 but got %d facets

What it means

In shortest-path queries with a weight facet, getCost reads exactly one facet value per edge to use as the edge cost. If more than one facet is present on the edge it can't decide which to use, so it returns this error and the facet (causing the path computation to fall back or fail).

Source

Thrown at query/shortest.go:123

func (sg *SubGraph) getCost(matrix, list int) (cost float64,
	fcs *pb.Facets, rerr error) {

	cost = 1.0
	if len(sg.facetsMatrix) <= matrix {
		return cost, fcs, rerr
	}
	fcsList := sg.facetsMatrix[matrix].FacetsList
	if len(fcsList) <= list {
		rerr = errFacet
		return cost, fcs, rerr
	}
	fcs = fcsList[list]
	if len(fcs.Facets) == 0 {
		rerr = errFacet
		return cost, fcs, rerr
	}
	if len(fcs.Facets) > 1 {
		rerr = errors.Errorf("Expected 1 but got %d facets", len(fcs.Facets))
		return cost, fcs, rerr
	}
	tv, err := facets.ValFor(fcs.Facets[0])
	if err != nil {
		return 0.0, nil, err
	}
	switch {
	case tv.Tid == types.IntID:
		cost = float64(tv.Value.(int64))
	case tv.Tid == types.FloatID:
		cost = tv.Value.(float64)
	default:
		rerr = errFacet
	}
	return cost, fcs, rerr
}

func (sg *SubGraph) expandOut(ctx context.Context,

View on GitHub (pinned to 759e242be6)

Solutions

  1. Ensure edges used for weighted shortest-path carry exactly one facet (the weight value)
  2. If an edge needs multiple attributes, move extras to a separate intermediate node or store weight in a dedicated facet set
  3. Inspect the offending edge's facets via a query returning facets and remove the surplus ones with a mutation (delete facet)
  4. Alternatively drop the weight facet from the shortest query so unweighted path is computed

Example fix

// before: edge with multiple facets used as weight
{
  path as shortest(from: 0x1, to: 0x2) {
    friend @facets(weight)
  }
}
// ensure only one facet exists, or query all facets and prune:
// after: keep only the weight facet on edges
{
  set {
    <0x1> <friend> <0x2> (weight=3.0) .
  }
}
// delete extra facets via:
// { set { <0x1> <friend> <0x2> (otherFacet=null) . } }
Defensive patterns

Strategy: validation

Validate before calling

// Ensure weight edges carry exactly one facet before shortest-path
facets, _ := txn.Query(ctx, `{ q(func: uid(0x1)) { friend @facets } }`)
if countFacetsOnEdges(facets) > 1 {
    return errors.New("weight edges must have exactly one facet")
}

Try / catch

resp, err := txn.Query(ctx, shortestDql)
if err != nil && strings.Contains(err.Error(), "Expected 1 but got") {
    // fall back to unweighted shortest path
    resp, err = txn.Query(ctx, unweightedShortestDql)
}

Prevention

When it happens

Trigger: Running a shortest path query with weight predicate facets (e.g. shortest weights: dgraph.facet or a weighted key) where an edge on the path has multiple facets attached, so len(fcs.Facets) > 1.

Common situations: Edges annotated with several facets (e.g. distance plus traffic) while the shortest query expects a single weight facet; accidentally adding extra facets to weight-carrying edges via mutations.

Related errors


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