{"record":{"id":"363deffb6e8005e1","repo":"vitessio/vitess","slug":"found-drop-constraint-v-but-could-not-find-cons","errorCode":null,"errorMessage":"Found DROP CONSTRAINT: %v, but could not find constraint name in map","messagePattern":"Found DROP CONSTRAINT: (.+?), but could not find constraint name in map","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/schemadiff/onlineddl.go","lineNumber":676,"sourceCode":"\n// ValidateAndEditAlterTableStatement inspects the AlterTable statement and:\n// - modifies any CONSTRAINT name according to given name mapping\n// - explode ADD FULLTEXT KEY into multiple statements\nfunc ValidateAndEditAlterTableStatement(originalTableName string, baseUUID string, capableOf capabilities.CapableOf, alterTable *sqlparser.AlterTable, constraintMap map[string]string) (alters []*sqlparser.AlterTable, err error) {\n\tcapableOfInstantDDLXtrabackup, err := capableOf(capabilities.InstantDDLXtrabackupCapability)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\thashExists := map[string]bool{}\n\tvalidateWalk := func(node sqlparser.SQLNode) (kontinue bool, err error) {\n\t\tswitch node := node.(type) {\n\t\tcase *sqlparser.DropKey:\n\t\t\tif node.Type == sqlparser.CheckKeyType || node.Type == sqlparser.ForeignKeyType || node.Type == sqlparser.ConstraintType {\n\t\t\t\t// drop a check or a foreign key constraint\n\t\t\t\tmappedName, ok := constraintMap[node.Name.String()]\n\t\t\t\tif !ok {\n\t\t\t\t\treturn false, fmt.Errorf(\"Found DROP CONSTRAINT: %v, but could not find constraint name in map\", sqlparser.CanonicalString(node))\n\t\t\t\t}\n\t\t\t\tnode.Name = sqlparser.NewIdentifierCI(mappedName)\n\t\t\t}\n\t\tcase *sqlparser.AddConstraintDefinition:\n\t\t\toldName := node.ConstraintDefinition.Name.String()\n\t\t\tnewName := newConstraintName(originalTableName, baseUUID, node.ConstraintDefinition, hashExists, sqlparser.CanonicalString(node.ConstraintDefinition.Details), oldName)\n\t\t\tnode.ConstraintDefinition.Name = sqlparser.NewIdentifierCI(newName)\n\t\t\tconstraintMap[oldName] = newName\n\t\t}\n\t\treturn true, nil\n\t}\n\tif err := sqlparser.Walk(validateWalk, alterTable); err != nil {\n\t\treturn alters, err\n\t}\n\talters = append(alters, alterTable)\n\t// Handle ADD FULLTEXT KEY statements\n\tcountAddFullTextStatements := 0\n\tredactedOptions := make([]sqlparser.AlterOption, 0, len(alterTable.AlterOptions))","sourceCodeStart":658,"sourceCodeEnd":694,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/schemadiff/onlineddl.go#L658-L694","documentation":"When rewriting a diffed ALTER/CREATE statement for Online DDL, the code maps renamed constraint names through constraintMap. A DROP CHECK / FOREIGN KEY / CONSTRAINT statement references a constraint name not present in the map, so the statement cannot be translated to the target's constraint naming. This indicates an internal inconsistency between the rename map and the parsed statements.","triggerScenarios":"Applying a diff containing `ALTER TABLE ... DROP CONSTRAINT/FOREIGN KEY/CHECK <name>` where constraintMap (built from the table's rename analysis) lacks that name.","commonSituations":"Renaming a table whose foreign-key constraints were renamed by a previous migration; a schema diff generated against a differently-named constraint set; partial migrations applied out of order.","solutions":["Report/verify the diff generation: the constraint rename map should include all dropped constraint names — this may be a schemadiff bug","Regenerate the schema diff from the current actual schemas so the map and statements are consistent","Drop and re-add the constraint explicitly in the migration so the name mapping is unnecessary"],"exampleFix":"// before: drop by stale name not in map\nALTER TABLE t DROP FOREIGN KEY fk_old;\n// after: drop and re-add so naming is explicit\nALTER TABLE t DROP FOREIGN KEY fk_old, ADD CONSTRAINT fk_new FOREIGN KEY (a) REFERENCES p(id);","handlingStrategy":"try-catch","validationCode":"for _, name := range droppedConstraintNames { if _, ok := constraintMap[name]; !ok { return fmt.Errorf(\"constraint %q not in rename map\", name) } }","typeGuard":null,"tryCatchPattern":"if err != nil {\n  if strings.Contains(err.Error(), \"could not find constraint name in map\") {\n    // regenerate the diff from current schemas or file a bug\n  }\n}","preventionTips":["Keep migrations linear — apply diffs generated from the current schema state","Prefer DROP + ADD for constraint renames to avoid mapping dependence","Track constraint names consistently across table renames"],"tags":["schemadiff","constraint","rename","online-ddl"],"backgroundTag":"constraint-name-mapping-failed","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}