Working with Enums

1. Defining Schema Enums

enum Role {
  USER
  EDITOR
  ADMIN @map("administrator")
}
AspectDetail
NamingPascalCase enum, UPPER_SNAKE values
Mapping@map per value, @@map for type name

2. Using Enum Fields

PatternExample
Requiredrole Role
Optionalrole Role?
Listroles Role[] (PG only)

3. Setting Enum Defaults

role Role @default(USER)
NoteDetail
MigrationDefault applied at DB level via DEFAULT clause
Change defaultRequires migration

4. Accessing Enum Values

import { Role } from "@prisma/client";
await prisma.user.create({ data: { email: "a@b.io", role: Role.ADMIN } });
SourceHow
Type-safe enumImported from generated client
String literalAlso accepted at runtime

5. Filtering by Enum Values

OperatorExample
Equality{ role: Role.ADMIN }
In{ role: { in: [Role.ADMIN, Role.EDITOR] } }
Not in{ role: { notIn: [Role.USER] } }
Not{ role: { not: Role.USER } }

6. Updating Enum Fields

await prisma.user.update({
  where: { id: 1 },
  data: { role: Role.EDITOR }
});
PatternNotes
Bulk updateupdateMany({ where:{role:USER}, data:{role:EDITOR} })
ConditionalUse where filters to scope

7. Migrating Enum Values

ChangeMigration Safety
Add valueSafe; appended to enum
RenameDrops + recreates; data migration needed
RemoveFails if rows still reference it
PG-specificGenerates ALTER TYPE ... ADD VALUE

8. Using Native Database Enums

DBBehavior
PostgreSQLGenerates real CREATE TYPE
MySQLInline ENUM column
SQLite/MongoStored as string

9. Handling Enum Type Safety

TipDetail
Exhaustive switchesTypeScript narrows enum unions
Zod / ValibotUse z.nativeEnum(Role)
Avoid as RoleValidate untrusted input

10. Converting Strings to Enums

function toRole(value: string): Role {
  if ((Object.values(Role) as string[]).includes(value)) return value as Role;
  throw new Error(`Invalid role: ${value}`);
}
StrategyDetail
Runtime guardValidate before passing to Prisma
Schema parsingUse Zod nativeEnum for DTOs