Foreign Key Repair Loop
This loop identifies and repairs broken foreign key relationships in a relational database. It systematically detects constraint violations, proposes correct…
This loop is republished with attribution. Full credit and the original content belong to the source linked above.
AI Skill Market Insights
Real data. Real impact.
Emerging
Developers
Kickoff Prompt
Copy this prompt and run it with your agent to start the loop.
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.