Foreign Key Repair Loop
This loop identifies and repairs broken foreign key relationships in a relational database. It systematically detects constraint violations, proposes corrective actions, and validates fixes until all foreign key checks pass.
Goal
Restore referential integrity by ensuring all foreign key constraints are valid across related tables
How to Run
Run this loop in your preferred coding agent environment. The agent will identify broken foreign keys, propose fixes, apply them incrementally, and verify improvements until all constraints pass or iteration limit is reached.
- 01
Initialize Agent Session
Start the loop in your coding agent with the kickoff prompt provided below
- 02
Monitor Iteration Progress
Each cycle should show analysis of broken constraints, proposed fixes, and validation results
- 03
Review Final Output
Confirm that all foreign key issues are resolved or note remaining violations for manual review
Workflow Steps
- 01
Detect broken foreign key relationships using database integrity check command
List of tables/columns with constraint violations is produced
- 02
Analyze each violation to determine root cause (missing parent, orphaned child, incorrect values)
Clear understanding of required data modifications documented
- 03
Generate corrective SQL statements (insert missing parents, update/delete orphaned children)
Proposed changes are safe and preserve data integrity
- 04
Apply changes within a transaction for safe rollback if needed
Changes are committed only after successful execution
- 05
Re-run integrity check to measure improvement
Fewer or no violations reported
- 06
Repeat cycle until exit condition met or max iterations reached
Loop terminates appropriately based on check results
Kickoff Prompt
Start the "Foreign Key Repair Loop" loop. Goal: Restore referential integrity by ensuring all foreign key constraints are valid across related tables Max iterations: 10 Between iterations run: Execute database integrity validation (e.g., `PRAGMA foreign_key_check` for SQLite, or equivalent for your RDBMS) Exit when: All foreign key constraints validate successfully with no orphaned or invalid references Begin by running the database integrity check command to identify any current foreign key violations. For each violation found, analyze whether it's due to missing parent records, orphaned children, or incorrect reference values. Generate appropriate SQL fixes (INSERT, UPDATE, or DELETE) to resolve each issue safely. Apply these changes within a transaction, then re-run the integrity check. Continue this cycle until all foreign key constraints pass validation or you reach 10 iterations. Prioritize preserving existing data over destructive operations. Self-pace this loop. After each iteration, run `Execute database integrity validation (e.g., `PRAGMA foreign_key_check` for SQLite, or equivalent for your RDBMS)` and evaluate the output, and only continue if the exit condition is not met (All foreign key constraints validate successfully with no orphaned or invalid references). Stop when the exit condition passes or 10 iterations are reached. Give a short status update each pass.
Guardrails
hardcoded- ·Back up the database before making any changes
- ·Never delete records without explicit confirmation from user
- ·Only modify constraints if explicitly authorized
- ·Flag circular or complex dependencies for manual inspection
- ·Rollback changes if any unexpected errors occur
Flow Diagram
Related loops — Database
Database
Backup Verification
Automates the verification of database backup restorability by repeatedly testing restore operations until successful or maximum iterations reached.
Database
Database Schema Sync Loop
Automatically detects and resolves schema drift between your database and ORM models by iteratively applying necessary migrations and verifying alignment.
Database
Query Performance Fixer
An automated workflow to identify, analyze, and optimize slow-running database queries through iterative benchmarking and targeted improvements.