Prisma String Types to SQL VARCHAR and TEXT
Understand how Prisma String fields map to SQL VARCHAR and TEXT types. Covers @db annotations for controlling exact SQL types across PostgreSQL, MySQL, and SQLite.
Detailed Explanation
String Type Mapping
Prisma's String type is a versatile text type that maps to different SQL column types depending on the dialect and any @db. annotations you provide.
Default Mapping
Without any @db. annotation, String maps to:
- PostgreSQL:
VARCHAR(255) - MySQL:
VARCHAR(255) - SQLite:
VARCHAR(255)
Using @db Annotations
You can control the exact SQL type using Prisma's native database type annotations:
model Article {
id Int @id @default(autoincrement())
title String @db.VarChar(200)
slug String @db.VarChar(100) @unique
content String @db.Text
code String @db.Char(6)
@@map("articles")
}
Generated SQL (PostgreSQL)
CREATE TABLE articles (
id SERIAL PRIMARY KEY,
title VARCHAR(200) NOT NULL,
slug VARCHAR(100) NOT NULL UNIQUE,
content TEXT NOT NULL,
code CHAR(6) NOT NULL
);
Annotation Reference
| Prisma annotation | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
String (default) |
VARCHAR(255) | VARCHAR(255) | VARCHAR(255) |
@db.Text |
TEXT | TEXT | TEXT |
@db.VarChar(n) |
VARCHAR(n) | VARCHAR(n) | VARCHAR(n) |
@db.Char(n) |
CHAR(n) | CHAR(n) | CHAR(n) |
@db.Uuid |
UUID | VARCHAR(36) | VARCHAR(36) |
UUID Fields
For UUID primary keys, Prisma's @db.Uuid annotation is particularly useful with PostgreSQL, which has a native UUID type:
model Session {
id String @id @default(uuid()) @db.Uuid
}
This generates id UUID PRIMARY KEY DEFAULT gen_random_uuid() for PostgreSQL.
Use Case
You are defining a blog platform schema in Prisma and need to ensure that the generated SQL uses TEXT for long-form content and VARCHAR with specific lengths for titles and slugs to match your existing database column specifications.