{"record":{"id":"c8e5174596f14fd9","repo":"vitessio/vitess","slug":"foreign-key-found-c8e517","errorCode":null,"errorMessage":"Foreign key found","messagePattern":"Foreign key found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/schemadiff/onlineddl.go","lineNumber":31,"sourceCode":"See the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\npackage schemadiff\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"math\"\n\t\"sort\"\n\t\"strings\"\n\n\t\"vitess.io/vitess/go/mysql/capabilities\"\n\t\"vitess.io/vitess/go/vt/sqlparser\"\n)\n\nvar (\n\tErrForeignKeyFound = errors.New(\"Foreign key found\")\n\n\tcopyAlgorithm = sqlparser.AlgorithmValue(sqlparser.CopyStr)\n)\n\nconst (\n\tmaxConstraintNameLength = 64\n)\n\ntype ConstraintType int\n\nconst (\n\tUnknownConstraintType ConstraintType = iota\n\tCheckConstraintType\n\tForeignKeyConstraintType\n)\n\nvar constraintIndicatorMap = map[int]string{\n\tint(CheckConstraintType):      \"chk\",","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/schemadiff/onlineddl.go#L13-L49","documentation":"ErrForeignKeyFound signals that onlineddl schema validation discovered a FOREIGN KEY constraint in a statement where foreign keys are not allowed. validateWalk and onlineDDLStatementSanity reject foreign keys because online DDL (gh-ost/online-schema-change migrations) cannot safely handle them under the configured migration setup.","triggerScenarios":"Submitting an ALTER/CREATE with a FOREIGN KEY through Online DDL while FK support is disabled; validating a CREATE TABLE containing FOREIGN KEY constraints via validateAndEditCreateTableStatement.","commonSituations":"Migrating legacy schemas that rely on FKs into Vitess Online DDL; clusters with --enable-external-foreign-keys disabled or MySQL flavors/capabilities lacking FK support for online migrations.","solutions":["Remove the FOREIGN KEY constraint from the statement, or run it as plain (non-online) DDL","Enable foreign key support for Online DDL in thevtctl/vttablet configuration if your setup supports it","Apply the FK in a separate migration step after the online migration completes"],"exampleFix":"// before\nALTER TABLE orders ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(id) -- ErrForeignKeyFound under online DDL\n// after\nALTER TABLE orders ADD COLUMN customer_id INT -- run FK add separately or via direct DDL","handlingStrategy":"validation","validationCode":"stmt, err := sqlparser.Parse(alterSQL)\n_ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {\n\tif ct, ok := node.(*sqlparser.CreateTable); ok {\n\t\tfor _, c := range ct.TableSpec.Constraints {\n\t\t\tif c.Check == nil { return false, fmt.Errorf(\"foreign key present: %s\", c.Name) }\n\t\t}\n\t}\n\treturn true, nil\n}, stmt)","typeGuard":null,"tryCatchPattern":"if err != nil {\n\tif errors.Is(err, schemadiff.ErrForeignKeyFound) {\n\t\t// reroute as non-online DDL or strip FK constraints\n\t}\n}","preventionTips":["Scan schema files for FOREIGN KEY before submitting Online DDL","Configure FK support explicitly if your setup allows it","Split FK-adding statements out of online migrations"],"tags":["onlineddl","foreign-key","migration"],"backgroundTag":"foreign-key-unsupported","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}