Convert SQL ENUM and CHECK Constraints to Prisma Enums
Learn how SQL ENUM types and CHECK constraints with fixed value lists convert to Prisma enum declarations. Covers PostgreSQL CREATE TYPE, MySQL ENUM, and CHECK-based enums.
Detailed Explanation
SQL Enums to Prisma Enums
Prisma has first-class support for enums via the enum keyword. SQL databases represent enumerations in different ways, and the converter handles each variant.
PostgreSQL CREATE TYPE
CREATE TYPE user_role AS ENUM ('admin', 'editor', 'viewer');
CREATE TYPE post_status AS ENUM ('draft', 'published', 'archived');
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
role user_role NOT NULL DEFAULT 'viewer'
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
status post_status NOT NULL DEFAULT 'draft',
author_id INTEGER NOT NULL REFERENCES users(id)
);
Generated Prisma Schema
enum UserRole {
admin
editor
viewer
}
enum PostStatus {
draft
published
archived
}
model User {
id Int @id @default(autoincrement())
name String
role UserRole @default(admin)
posts Post[]
@@map("users")
}
model Post {
id Int @id @default(autoincrement())
title String
status PostStatus @default(draft)
authorId Int @map("author_id")
author User @relation(fields: [authorId], references: [id])
@@map("posts")
}
MySQL ENUM Column
MySQL defines enums inline:
CREATE TABLE tickets (
id INT AUTO_INCREMENT PRIMARY KEY,
priority ENUM('low', 'medium', 'high', 'critical') NOT NULL DEFAULT 'medium'
);
The converter extracts the values and creates a Prisma enum:
enum TicketPriority {
low
medium
high
critical
}
CHECK Constraint to Enum
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
status VARCHAR(20) NOT NULL,
CONSTRAINT chk_status CHECK (status IN ('pending', 'processing', 'shipped', 'delivered'))
);
The converter parses the CHECK constraint's IN (...) list and generates a Prisma enum.
Enum Naming
| SQL name | Prisma enum name |
|---|---|
user_role |
UserRole (PascalCase) |
post_status |
PostStatus |
Inline MySQL ENUM on priority |
TicketPriority ({Model}{Field}) |
Value Mapping
Prisma enum values must be valid identifiers. If SQL enum values contain special characters or start with numbers, the converter uses @map:
enum HttpMethod {
GET
POST
PUT
DELETE @map("delete")
}
Migration Considerations
Adding a new value to a Prisma enum generates an ALTER TYPE ... ADD VALUE migration for PostgreSQL. Removing values is not directly supported — you must create a new enum and migrate data. Plan your enums with future extensibility in mind.
Use Case
Your PostgreSQL database uses CREATE TYPE for roles and statuses, and your MySQL database uses inline ENUM columns. The converter normalizes both patterns into Prisma enum declarations with correct defaults and model references.
Try It — SQL to Prisma Schema
Related Topics
Convert a Simple SQL Table to a Prisma Model
Basic Models
Map SQL DEFAULT Clauses to Prisma @default Attributes
Advanced Features
Convert SQL UNIQUE Constraints to Prisma @unique and @@unique
Advanced Features
Convert a SQL User Authentication Schema to Prisma
Real-World Schemas
Convert an E-Commerce SQL Schema to Prisma
Real-World Schemas