# Add the missing foreign key that documents a relationship

A relationship the code assumes and the database never declared, made explicit once the rows with no match are fixed.

Category: Data layer and migrations
Source: https://letsship.ai/use-cases/add-the-missing-foreign-key
Published: 2026-08-25

## Goal

Add the foreign key constraint for a relationship the code already assumes.

## Acceptance criteria

- Violating rows are reported and resolved before the constraint is added
- The constraint is added without an unacceptable lock
- The delete behaviour is chosen deliberately and stated
- A test asserts the constraint rejects an orphan

## What lands as proof

A count of violating rows before the fix, and a test proving the constraint now refuses one.

## Prompt

Copy this to an agent to open the ticket:

```text
Create a Linear issue in my team and assign it to the SHIP agent.

Title: Add the foreign key between {child} and {parent}

Description: Add the foreign key constraint for a relationship the code already assumes.

Acceptance criteria:
- Violating rows are reported and resolved before the constraint is added
- The constraint is added without an unacceptable lock
- The delete behaviour is chosen deliberately and stated
- A test asserts the constraint rejects an orphan

Before you create the issue, find a relationship in this repository enforced only in code and replace {child} and {parent}.
```

## Questions

### What does the agent actually change?

The ticket is scoped to one outcome: add the foreign key constraint for a relationship the code already assumes. Work that serves that outcome is in scope, and anything outside it is left for a separate ticket, so the pull request stays reviewable.

### How do I know the work is done?

The pull request carries the evidence, not only the diff. Here that means violating rows reported and resolved before the constraint lands, so a reviewer can confirm the result without reproducing the work locally.

### How much oversight does this need?

The run stops once the plan is written. Somebody reads the approach and approves it before any code exists, which is the cheapest moment to redirect the work.
