Databaseprompt onlyIntermediate

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.

← all loops
database-integrityforeign-key-repairrelational-databasedata-consistencyconstraint-validation

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.

  1. 01

    Initialize Agent Session

    Start the loop in your coding agent with the kickoff prompt provided below

  2. 02

    Monitor Iteration Progress

    Each cycle should show analysis of broken constraints, proposed fixes, and validation results

  3. 03

    Review Final Output

    Confirm that all foreign key issues are resolved or note remaining violations for manual review

Workflow Steps

  1. 01

    Detect broken foreign key relationships using database integrity check command

    List of tables/columns with constraint violations is produced

  2. 02

    Analyze each violation to determine root cause (missing parent, orphaned child, incorrect values)

    Clear understanding of required data modifications documented

  3. 03

    Generate corrective SQL statements (insert missing parents, update/delete orphaned children)

    Proposed changes are safe and preserve data integrity

  4. 04

    Apply changes within a transaction for safe rollback if needed

    Changes are committed only after successful execution

  5. 05

    Re-run integrity check to measure improvement

    Fewer or no violations reported

  6. 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

rendering…

Related loops — Database