{"record":{"id":"c50e42e71f6fe1bd","repo":"theonedev/onedev","slug":"failed-to-apply-database-constraints-if-this-erro","errorCode":null,"errorMessage":"Failed to apply database constraints. If this error is caused by foreign key constraint violations, you may fix it via your database sql tool, and then run apply-db-constraints to reapply database constraints","messagePattern":"Failed to apply database constraints\\. If this error is caused by foreign key constraint violations, you may fix it via your database sql tool, and then run apply-db-constraints to reapply database constraints","errorType":"console","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"server-core/src/main/java/io/onedev/server/commandhandler/RestoreDatabase.java","lineNumber":133,"sourceCode":"\t\t\t});\n\t\t} catch (SQLException e) {\n\t\t\tthrow new RuntimeException(e);\n\t\t}\n\t\t\t\t\n\t\tlogger.info(\"Importing data into database...\");\n\t\tdataService.importData(dataDir);\n\n\t\ttry (var conn = dataService.openConnection()) {\n\t\t\tcallWithTransaction(conn, () -> {\n\t\t\t\tlogger.info(\"Applying foreign key constraints...\");\n\t\t\t\ttry {\n\t\t\t\t\tdataService.applyConstraints(conn);\n\t\t\t\t} catch (Exception e) {\n\t\t\t\t\tvar message = String.format(\"Failed to apply database constraints. If this error is caused by \" +\n\t\t\t\t\t\t\t\"foreign key constraint violations, you may fix it via your database sql tool, and \" +\n\t\t\t\t\t\t\t\"then run %s to reapply database constraints\", \n\t\t\t\t\t\t\tCommand.getScript(\"apply-db-constraints\"));\n\t\t\t\t\tthrow new RuntimeException(message);\n\t\t\t\t}\n\t\t\t\treturn null;\n\t\t\t});\n\t\t} catch (SQLException e) {\n\t\t\tthrow new RuntimeException(e);\n\t\t}\n\t}\n\n\t@Override\n\tpublic void stop() {\n\t\tsessionFactoryService.stop();\n\t}\n\t\n}\n","sourceCodeStart":115,"sourceCodeEnd":148,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/commandhandler/RestoreDatabase.java#L115-L148","documentation":"During a database restore, RestoreDatabase.doRestore calls dataService.applyConstraints(conn) to reapply foreign key and other constraints to the restored data. If that fails for any reason — typically because the restored dump contains rows violating foreign keys — it wraps the failure in a RuntimeException advising the user to fix violations with a SQL tool and re-run the apply-db-constraints command.","triggerScenarios":"Running the 'restore' command where the imported data violates database constraints (orphaned foreign keys, missing referenced rows), causing DataService.applyConstraints to fail during doRestore.","commonSituations":"Restoring a backup produced from a database where constraints had been previously dropped/relaxed; hand-edited or partially imported dumps; migrating between database engines with different constraint semantics; interrupted restore leaving partial data.","solutions":["Follow the message: fix foreign key violations with a SQL tool (delete/update orphaned rows), then run the apply-db-constraints script to reapply constraints.","Identify violating rows by querying child rows whose foreign keys have no parent match.","Re-create the restore from a clean, consistent backup instead of patching a corrupt dump.","Ensure the source and target database engines/versions are compatible so constraint SQL applies cleanly."],"exampleFix":"-- find orphaned rows before reapplying constraints\nSELECT c.* FROM issue c LEFT JOIN project p ON c.project_id = p.id\nWHERE p.id IS NULL;\nDELETE FROM issue WHERE project_id NOT IN (SELECT id FROM project);\n-- then in shell\n./apply-db-constraints","handlingStrategy":"retry","validationCode":"-- detect orphaned foreign keys before reapplying constraints\nSELECT child.* FROM child_table child\nLEFT JOIN parent_table p ON child.parent_id = p.id\nWHERE p.id IS NULL;","typeGuard":null,"tryCatchPattern":"try {\n    restoreCommand.start();\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"Failed to apply database constraints\"))\n    logger.error(\"Fix FK violations in DB, then run apply-db-constraints\");\n}","preventionTips":["Take backups from databases with constraints fully applied and data consistent.","Never hand-edit restored dumps without re-checking referential integrity.","Restore into the same (or compatible) database engine/version the dump came from.","After any constraint failure, run the apply-db-constraints script after cleaning orphan rows."],"tags":["database","foreign-key","restore","data-integrity"],"backgroundTag":"database-constraint-violation","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}