Prisma Enum to SQL CREATE TYPE and ENUM
Convert Prisma enum definitions to PostgreSQL CREATE TYPE, MySQL inline ENUM, or SQLite CHECK constraints. Learn dialect-specific enum handling.
Detailed Explanation
Enum Conversion by Dialect
Prisma enums are a way to define a set of allowed values for a field. Each SQL dialect handles enums differently, and the converter produces the correct syntax for each.
Example Prisma Schema
enum Status {
DRAFT
PUBLISHED
ARCHIVED
}
model Article {
id Int @id @default(autoincrement())
title String
status Status @default(DRAFT)
@@map("articles")
}
PostgreSQL Output
CREATE TYPE status AS ENUM (
'DRAFT',
'PUBLISHED',
'ARCHIVED'
);
CREATE TABLE articles (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
status status NOT NULL DEFAULT 'DRAFT'
);
PostgreSQL has a native CREATE TYPE ... AS ENUM feature that creates a true enumerated type. The column type references this named type.
MySQL Output
CREATE TABLE articles (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
status ENUM('DRAFT', 'PUBLISHED', 'ARCHIVED') NOT NULL DEFAULT 'DRAFT'
);
MySQL uses an inline ENUM(...) column type. The allowed values are listed directly in the column definition.
SQLite Output
CREATE TABLE articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title VARCHAR(255) NOT NULL,
status TEXT NOT NULL DEFAULT 'DRAFT',
CHECK (status IN ('DRAFT', 'PUBLISHED', 'ARCHIVED'))
);
SQLite has no native enum type. The converter uses a TEXT column with a CHECK constraint to enforce the allowed values.
Multiple Enums
You can define as many enums as needed. Each generates its own CREATE TYPE (PostgreSQL) or is used inline (MySQL/SQLite). Enum names follow the same snake_case conversion rules as table names.
Use Case
You are building a content management system with articles that go through a draft-published-archived lifecycle, and need to ensure the database enforces valid status values across all three supported database engines.