{"record":{"id":"d83293783811241a","repo":"dgraph-io/dgraph","slug":"expected-1-but-got-d-facets","errorCode":null,"errorMessage":"Expected 1 but got %d facets","messagePattern":"Expected 1 but got (.+?) facets","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/shortest.go","lineNumber":123,"sourceCode":"func (sg *SubGraph) getCost(matrix, list int) (cost float64,\n\tfcs *pb.Facets, rerr error) {\n\n\tcost = 1.0\n\tif len(sg.facetsMatrix) <= matrix {\n\t\treturn cost, fcs, rerr\n\t}\n\tfcsList := sg.facetsMatrix[matrix].FacetsList\n\tif len(fcsList) <= list {\n\t\trerr = errFacet\n\t\treturn cost, fcs, rerr\n\t}\n\tfcs = fcsList[list]\n\tif len(fcs.Facets) == 0 {\n\t\trerr = errFacet\n\t\treturn cost, fcs, rerr\n\t}\n\tif len(fcs.Facets) > 1 {\n\t\trerr = errors.Errorf(\"Expected 1 but got %d facets\", len(fcs.Facets))\n\t\treturn cost, fcs, rerr\n\t}\n\ttv, err := facets.ValFor(fcs.Facets[0])\n\tif err != nil {\n\t\treturn 0.0, nil, err\n\t}\n\tswitch {\n\tcase tv.Tid == types.IntID:\n\t\tcost = float64(tv.Value.(int64))\n\tcase tv.Tid == types.FloatID:\n\t\tcost = tv.Value.(float64)\n\tdefault:\n\t\trerr = errFacet\n\t}\n\treturn cost, fcs, rerr\n}\n\nfunc (sg *SubGraph) expandOut(ctx context.Context,","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/shortest.go#L105-L141","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure edges used for weighted shortest-path carry exactly one facet (the weight value)","If an edge needs multiple attributes, move extras to a separate intermediate node or store weight in a dedicated facet set","Inspect the offending edge's facets via a query returning facets and remove the surplus ones with a mutation (delete facet)","Alternatively drop the weight facet from the shortest query so unweighted path is computed"],"exampleFix":"// before: edge with multiple facets used as weight\n{\n  path as shortest(from: 0x1, to: 0x2) {\n    friend @facets(weight)\n  }\n}\n// ensure only one facet exists, or query all facets and prune:\n// after: keep only the weight facet on edges\n{\n  set {\n    <0x1> <friend> <0x2> (weight=3.0) .\n  }\n}\n// delete extra facets via:\n// { set { <0x1> <friend> <0x2> (otherFacet=null) . } }","handlingStrategy":"validation","validationCode":"// Ensure weight edges carry exactly one facet before shortest-path\nfacets, _ := txn.Query(ctx, `{ q(func: uid(0x1)) { friend @facets } }`)\nif countFacetsOnEdges(facets) > 1 {\n    return errors.New(\"weight edges must have exactly one facet\")\n}","typeGuard":null,"tryCatchPattern":"resp, err := txn.Query(ctx, shortestDql)\nif err != nil && strings.Contains(err.Error(), \"Expected 1 but got\") {\n    // fall back to unweighted shortest path\n    resp, err = txn.Query(ctx, unweightedShortestDql)\n}","preventionTips":["Enforce single-facet writes on weight predicates in your mutation layer","Audit edges for stray facets after schema changes","Use a dedicated intermediate node if edges need multiple attributes"],"tags":["shortest-path","facets","weighted-graph","dgraph"],"backgroundTag":"invalid-facet-count","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}