{"record":{"id":"d304f89441d1763f","repo":"vitessio/vitess","slug":"unexpected-error-cannot-find-diff-v","errorCode":null,"errorMessage":"unexpected error: cannot find diff: %v","messagePattern":"unexpected error: cannot find diff: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/schemadiff/schema_diff.go","lineNumber":379,"sourceCode":"\t\t\tConflictingDiffs: d.UnorderedDiffs(),\n\t\t}\n\t}\n\tlastGoodSchema := d.schema.copy()\n\tvar orderedDiffs []EntityDiff\n\tm := d.r.Map()\n\n\tunorderedDiffsMap := map[string]int{}\n\tfor i, diff := range d.UnorderedDiffs() {\n\t\tunorderedDiffsMap[diff.CanonicalStatementString()] = i\n\t}\n\t// The order of classes in the equivalence relation is, generally speaking, loyal to the order of original diffs.\n\tfor _, class := range d.r.OrderedClasses() {\n\t\tclassDiffs := []EntityDiff{}\n\t\t// Which diffs are in this equivalence class?\n\t\tfor _, statementString := range m[class] {\n\t\t\tdiff, ok := d.diffByStatementString(statementString)\n\t\t\tif !ok {\n\t\t\t\treturn nil, fmt.Errorf(\"unexpected error: cannot find diff: %v\", statementString)\n\t\t\t}\n\t\t\tclassDiffs = append(classDiffs, diff)\n\t\t}\n\t\tsort.SliceStable(classDiffs, func(i, j int) bool {\n\t\t\treturn unorderedDiffsMap[classDiffs[i].CanonicalStatementString()] < unorderedDiffsMap[classDiffs[j].CanonicalStatementString()]\n\t\t})\n\n\t\t// We will now permutate the diffs in this equivalence class, and hopefully find\n\t\t// a valid permutation (one where if we apply the diffs in-order, the schema remains valid throughout the process)\n\t\ttryPermutateDiffs := func(hints *DiffHints) (bool, error) {\n\t\t\treturn permutateDiffs(ctx, classDiffs, hints, func(permutatedDiffs []EntityDiff, hints *DiffHints) (bool, int) {\n\t\t\t\tpermutationSchema := lastGoodSchema.copy()\n\t\t\t\t// We want to apply the changes one by one, and validate the schema after each change\n\t\t\t\tfor i := range permutatedDiffs {\n\t\t\t\t\t// apply inline\n\t\t\t\t\tapplyHints := hints\n\t\t\t\t\tif hints.ForeignKeyCheckStrategy == ForeignKeyCheckStrategyCreateTableFirst {\n\t\t\t\t\t\t// This is a strategy that handles foreign key loops in a heuristic way.","sourceCodeStart":361,"sourceCodeEnd":397,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/schemadiff/schema_diff.go#L361-L397","documentation":"OrderedDiffs reorders an EntityDiff set by looking up each statement string produced by OrderedClasses() in the diff set's internal map. If a class's statement cannot be found, this internal-consistency error is returned. It almost always signals a bug in schemadiff or use of an API against a stale/partially constructed diff object, not a user schema problem.","triggerScenarios":"Calling OrderedDiffs where m[class] contains a statement string for which d.diffByStatementString returns !ok — i.e. the class grouping and the diff map are out of sync.","commonSituations":"Custom tooling consuming schemadiff internals against a modified or cached diff; a genuine schemadiff defect in grouping/canonicalization (e.g. canonical statement string mismatch after normalization changes).","solutions":["Re-run the diff generation from scratch so the diff set and ordering groups come from the same run","If reproducible, file a vitess bug with the schemas and diff output — this is an internal invariant violation","Upgrade to a newer vitess version where normalization/canonicalization bugs may be fixed"],"exampleFix":"// before: reusing a stale diff object across schema versions\nordered, err := staleDiff.OrderedDiffs()\n// after\nfreshDiff, err := schemaDiff.FromSchemas(ctx, from, to)\nordered, err := freshDiff.OrderedDiffs()","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"ordered, err := diff.OrderedDiffs()\nif err != nil {\n  if strings.Contains(err.Error(), \"cannot find diff\") {\n    // regenerate the diff from scratch and retry once\n  }\n}","preventionTips":["Never reuse EntityDiff objects across schema versions","Keep schemadiff version consistent between producer and consumer","Report persistent occurrences as vitess bugs"],"tags":["schemadiff","internal","diff"],"backgroundTag":"internal-invariant-violation","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}