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.
Restore referential integrity by ensuring all foreign key constraints are valid across related tables
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.
Initialize Agent Session
Start the loop in your coding agent with the kickoff prompt provided below
Monitor Iteration Progress
Each cycle should show analysis of broken constraints, proposed fixes, and validation results
Review Final Output
Confirm that all foreign key issues are resolved or note remaining violations for manual review
Detect broken foreign key relationships using database integrity check command
List of tables/columns with constraint violations is produced
Analyze each violation to determine root cause (missing parent, orphaned child, incorrect values)
Clear understanding of required data modifications documented
Database
Automates the verification of database backup restorability by repeatedly testing restore operations until successful or maximum iterations reached.
Database
Automatically detects and resolves schema drift between your database and ORM models by iteratively applying necessary migrations and verifying alignment.
Database
An automated workflow to identify, analyze, and optimize slow-running database queries through iterative benchmarking and targeted improvements.
Generate corrective SQL statements (insert missing parents, update/delete orphaned children)
Proposed changes are safe and preserve data integrity
Apply changes within a transaction for safe rollback if needed
Changes are committed only after successful execution
Re-run integrity check to measure improvement
Fewer or no violations reported
Repeat cycle until exit condition met or max iterations reached
Loop terminates appropriately based on check results
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.