Convert SQL Foreign Keys to Prisma One-to-Many Relations
Learn how SQL FOREIGN KEY constraints translate to Prisma one-to-many relation fields. Covers @relation, references, onDelete, and back-references.
Detailed Explanation
One-to-Many Relations
The most common SQL relationship is one-to-many: one parent record has many child records via a foreign key. Prisma models this with relation fields on both sides.
Example SQL
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT,
author_id INTEGER NOT NULL,
CONSTRAINT fk_posts_author FOREIGN KEY (author_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE INDEX idx_posts_author_id ON posts (author_id);
Generated Prisma Schema
model User {
id Int @id @default(autoincrement())
name String
posts Post[]
@@map("users")
}
model Post {
id Int @id @default(autoincrement())
title String
content String? @db.Text
authorId Int @map("author_id")
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
@@index([authorId])
@@map("posts")
}
How the Conversion Works
- Foreign key column (
author_id) becomes a scalar fieldauthorId Intwith@map. - Relation field (
author) is added asUsertype with@relation(...)specifying the link. - Back-reference (
posts Post[]) is added on the parent model. The[]indicates one-to-many. - ON DELETE action maps to Prisma's
onDeleteparameter.
Referential Actions
| SQL action | Prisma action | Behavior |
|---|---|---|
ON DELETE CASCADE |
Cascade |
Delete children when parent is deleted |
ON DELETE SET NULL |
SetNull |
Set foreign key to null |
ON DELETE RESTRICT |
Restrict |
Prevent parent deletion |
ON DELETE SET DEFAULT |
SetDefault |
Reset to default value |
ON DELETE NO ACTION |
NoAction |
Database checks at transaction end |
Naming the Relation
When a model has multiple relations to the same target, Prisma requires a relation name:
model Post {
authorId Int
author User @relation("PostAuthor", fields: [authorId], references: [id])
reviewerId Int?
reviewer User? @relation("PostReviewer", fields: [reviewerId], references: [id])
}
The converter detects multiple foreign keys to the same table and auto-generates descriptive relation names.
Index on Foreign Key
Prisma recommends an explicit @@index on foreign key fields for query performance. The converter adds this automatically, mirroring any CREATE INDEX on the FK column.
Use Case
You have a blog database where users author many posts via a foreign key. The converter generates Prisma relation fields on both models, maps the ON DELETE CASCADE action, and adds the necessary index.
Try It — SQL to Prisma Schema
Related Topics
Convert SQL Junction Tables to Prisma Many-to-Many Relations
Relations
Convert SQL One-to-One Foreign Keys to Prisma Relations
Relations
Map SQL Primary Key Strategies to Prisma @id
Basic Models
Convert SQL CREATE INDEX to Prisma @@index
Advanced Features
Convert a SQL User Authentication Schema to Prisma
Real-World Schemas