Group
  • Database

    Add foreign keys and decide delete behavior

    Enforces relationships at the database level and makes deletes cascade, restrict, or null out deliberately.

    • Improve quality
    • Plan & validate
    • Intermediate
    • General

Free Prompt

Audit the relationships in my database and enforce them with foreign keys. Look at every field that references another record (user_id on posts, order_id on line items, team_id on members) and check whether a real constraint exists. For each relationship, add the foreign key and choose the delete behavior deliberately: CASCADE where the child genuinely can't exist without the parent (line items of an order), RESTRICT or prevent deletion where the child should survive and deleting the parent should be blocked (orders referencing a user), and SET NULL where the child can stand alone with a missing reference (posts whose author was anonymized). Document each choice with one line of reasoning. In document databases without real foreign keys, add the equivalent application-level checks at delete time plus a periodic cleanup job for orphans. Before adding constraints, find orphaned rows that would violate them and report them to me for cleanup. Do not cascade deletes anywhere that could wipe out large amounts of data unexpectedly; flag any cascade that could delete more than a handful of rows at once. Deliver: the relationship map with delete behavior per relationship, the orphan report, and a verification: attempt to delete a parent that has restricted children and confirm it fails cleanly.

What This Does / How This Helps

Turns implied relationships into enforced ones, with a deliberate answer for what happens to children when a parent gets deleted. Without foreign keys, databases fill with orphans: posts by deleted users, line items for orders that don't exist, memberships pointing at teams that were removed months ago. The app then crashes or renders garbage when it follows a reference into nothing, usually on the page a user cares about most. The delete-behavior decision per relationship is the real work here, and the prompt forces it: cascade, restrict, or null out, each with a reason, so 'what happens when a user deletes their account' has an actual answer.

Want to skip doing this by hand?

Fortivibe audits your app for all of the areas these prompts cover (and more).

See What We Check

Related Prompts