Understanding Prisma Schema Structure

1. Defining Datasource Block

FieldRequiredNotes
providerYesDB engine name
urlYesConnection string or env("...")
directUrlNoBypass pooler for migrations
shadowDatabaseUrlNoFor migration shadow DB
relationModeNo"foreignKeys" | "prisma"
schemasNoPG multi-schema (preview)

2. Configuring Generator Block

FieldDescription
providerprisma-client-js, prisma-client (ESM), custom
outputPath to write generated client
previewFeaturesArray of opt-in feature names
binaryTargetsCompile targets
engineTypelibrary | binary | client

3. Creating Model Definitions

ElementSyntax
Keywordmodel ModelName { ... }
Fieldname Type modifier? @attribute
NamingPascalCase models, camelCase fields
DB mapping@@map("table_name")

Example: Minimal model

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String?
  createdAt DateTime @default(now())
  posts     Post[]
  @@map("users")
}

4. Defining Enum Types

AspectNotes
Syntaxenum Name { VALUE1 VALUE2 }
NamingPascalCase enum, UPPER_SNAKE values
MappingVALUE @map("db_value")
SQLiteNot supported (use String)

5. Using Comments and Documentation

SyntaxPurpose
// commentDeveloper comment (ignored)
/// doc commentTriple-slash, appears in client JSDoc

6. Understanding Schema Syntax Rules

RuleDetail
One datasourceOnly a single datasource block allowed
Multiple generatorsAllowed (e.g., client + erd)
No semicolonsWhitespace-delimited
Case sensitiveIdentifiers and attributes
Reserved namesAvoid PrismaClient, Prisma as model names

7. Organizing Multi-File Schemas

ConceptNotes
Preview featureprismaSchemaFolder
Layoutprisma/schema/*.prisma
Cross-file refsModels across files resolve automatically
CLI--schema prisma/schema

8. Validating Schema Syntax

npx prisma validate
npx prisma validate --schema=./prisma/schema.prisma
CheckDetected
Syntax errorsBad tokens, missing braces
Missing relationsDangling back-references
Type mismatchesIncompatible @db types

9. Formatting Schema Files

CommandEffect
prisma formatAligns columns, normalizes whitespace
VS CodePrisma extension formats on save

10. Using Schema Extensions

ExtensionUse
PostgreSQL extensionsDeclare via extensions = [pgcrypto, citext] (preview)
Custom generatorse.g., prisma-erd-generator, prisma-zod-generator