# How to Fix AI Generating Invalid Prisma Relations

> AI agents generate Prisma schema relations with missing back-relations, wrong referential actions, or mismatched field types that fail prisma validate and prisma migrate.

**Type:** Failure  
**Tools:** Cursor, Claude Code, Codex, Windsurf  
**Stack:** PostgreSQL, TypeScript  
**Updated:** 2026-06-08

---

The agent writes a Prisma schema with relations that look correct but fail
`prisma validate` due to missing back-references, type mismatches, or
ambiguous multi-relation setups.

## The symptom

A relation is declared on one model without its required counterpart on the
other, or the foreign key field type doesn't match the referenced field.

```prisma
// schema.prisma — WRONG
model User {
  id    String @id @default(cuid())
  posts Post[]
}

model Post {
  id       String @id @default(cuid())
  authorId Int    // Int, but User.id is String — type mismatch
  // Missing: author User @relation(fields: [authorId], references: [id])
}
```

Running `prisma validate` produces:

```
Error: The relation field `posts` on model `User` is missing an opposite
relation field on the model `Post`.
```

## Why it happens

The agent often writes one side of a relation and forgets to add the other,
especially in many-to-many or self-referential cases. It also copies field types
from memory without checking that `authorId` must match the referenced `id`
type exactly.

## How to spot it

- `prisma validate` or `prisma migrate dev` fails with a relation error.
- A model has an array field (one-to-many) but the referenced model has no
  scalar foreign key or `@relation` attribute.
- Many-to-many uses explicit join tables with mismatched types.
- Self-referential models (e.g. category trees) have only one side of the
  `@relation(name: "...")` pair.

## How to fix it

Every relation must have both sides declared. Foreign key scalar types must
match the referenced field type exactly.

```prisma
// schema.prisma — CORRECT
model User {
  id    String @id @default(cuid())
  posts Post[] // one-to-many: back-reference
}

model Post {
  id       String @id @default(cuid())
  authorId String                       // String, matches User.id
  author   User   @relation(fields: [authorId], references: [id], onDelete: Cascade)
}
```

For a self-referential tree:

```prisma
model Category {
  id       String     @id @default(cuid())
  parentId String?
  parent   Category?  @relation("CategoryTree", fields: [parentId], references: [id])
  children Category[] @relation("CategoryTree")
}
```

```txt
[ ] Run "prisma validate" before "prisma migrate dev" — fix all errors first
[ ] Every @relation on model A has a matching field on model B
[ ] Foreign key scalar type matches the referenced @id type (String/Int/cuid)
[ ] Self-referential relations use a named @relation("name") on both sides
[ ] Explicit many-to-many join tables declare both foreign keys with onDelete
[ ] onDelete referential action is explicit (Cascade, Restrict, SetNull) — not left to default
```

## Fix Prompt

```txt title="Fix Prompt"
This Prisma schema fails prisma validate. Fix every relation error: add the
missing back-reference fields, ensure all foreign key scalar types exactly match
the referenced @id type, give every self-referential relation a unique name on
both sides, and set explicit onDelete actions (Cascade for required relations,
SetNull for optional). Run prisma validate after each change until it passes.
```

## Test

```bash
# Validate schema before attempting migration
npx prisma validate && echo "Schema OK" || echo "FAIL: schema invalid"

# Also check for type mismatches (Int fk -> String id is a common error)
grep -A3 "@relation" prisma/schema.prisma
```