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.
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.
// schema.prisma — WRONGmodel 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 oppositerelation 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 validateorprisma migrate devfails with a relation error.- A model has an array field (one-to-many) but the referenced model has no
scalar foreign key or
@relationattribute. - 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.
// schema.prisma — CORRECTmodel 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:
model Category { id String @id @default(cuid()) parentId String? parent Category? @relation("CategoryTree", fields: [parentId], references: [id]) children Category[] @relation("CategoryTree")}[ ] 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 defaultFix Prompt
This Prisma schema fails prisma validate. Fix every relation error: add themissing back-reference fields, ensure all foreign key scalar types exactly matchthe referenced @id type, give every self-referential relation a unique name onboth sides, and set explicit onDelete actions (Cascade for required relations,SetNull for optional). Run prisma validate after each change until it passes.Test
# Validate schema before attempting migrationnpx 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