dgraph-io/dgraph · error

value of to var(%s) should have already been populated

Error message

value of to var(%s) should have already been populated

What it means

Analogous to the from-var case: for shortest-path queries the 'to' endpoint supplied via a variable must already exist in the computed variables map. If the named variable is missing from mp, Dgraph throws this error because the destination was never populated.

Source

Thrown at query/query.go:1791

		fromVar := sg.Params.ShortestPathArgs.From.NeedsVar[0].Name
		uidVar, ok := mp[fromVar]
		if !ok {
			return errors.Errorf("value of from var(%s) should have already been populated",
				fromVar)
		}
		if uidVar.Uids != nil && len(uidVar.Uids.Uids) > 0 {
			if len(uidVar.Uids.Uids) > 1 {
				return errors.Errorf("from variable(%s) should only expand to 1 uid", fromVar)
			}
			sg.Params.From = uidVar.Uids.Uids[0]
		}
	}

	if sg.Params.ShortestPathArgs.To != nil && len(sg.Params.ShortestPathArgs.To.NeedsVar) > 0 {
		toVar := sg.Params.ShortestPathArgs.To.NeedsVar[0].Name
		uidVar, ok := mp[toVar]
		if !ok {
			return errors.Errorf("value of to var(%s) should have already been populated",
				toVar)
		}
		if uidVar.Uids != nil && len(uidVar.Uids.Uids) > 0 {
			if len(uidVar.Uids.Uids) > 1 {
				return errors.Errorf("to variable(%s) should only expand to 1 uid", toVar)
			}
			sg.Params.To = uidVar.Uids.Uids[0]
		}
	}
	return nil
}

// fillVars reads the value corresponding to a variable from the map mp and stores it inside
// SubGraph. This value is then later used for execution of the SubGraph.
func (sg *SubGraph) fillVars(mp map[string]varValue) error {
	if sg.Params.Alias == "shortest" {
		if err := sg.fillShortestPathVars(mp); err != nil {
			return err

View on GitHub (pinned to 759e242be6)

Solutions

  1. Define the to variable with 'as' in the same query block before consumption
  2. Check spelling of the variable in shortest(to: var(...))
  3. Ensure the emitting clause matches at least one node
  4. Reorder queries so the variable is materialized before the shortest-path subgraph runs

Example fix

// before: var(tgt) undefined
{
  p(func: uid(0x1)) { shortest(to: var(tgt)) { uid } }
}
// after
{
  tgt as var(func: eq(name, "bob"))
  p(func: uid(0x1)) { shortest(to: var(tgt)) { uid } }
}
Defensive patterns

Strategy: validation

Validate before calling

for _, v := range referencedVars(query, "shortest", "to") {
    if !definedBefore(query, v) {
        return fmt.Errorf("to-variable %s must be defined before shortest()", v)
    }
}

Try / catch

if err != nil && strings.Contains(err.Error(), "value of to var") {
    return fmt.Errorf("to-variable missing or empty; check its definition: %w", err)
}

Prevention

When it happens

Trigger: shortest(..., to: var(t)) where t was never defined with 'as', was defined later than the shortest-path block, or its emitting clause matched nothing so it never entered the done-vars map.

Common situations: Typo in the to-variable name; variable emitted only inside a conditional branch; query restructured so the shortest-path block executes before the variable is defined.

Related errors


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