Defining Schema Models

1. Creating Model Definitions

ElementRequiredNotes
Identifier fieldYes@id or @@id
Unique fieldNoRecommended for lookups
TimestampsNocreatedAt / updatedAt convention

2. Defining Field Names

RuleDetail
ConventioncamelCase
Allowed charsLetters, digits, underscore; must start with letter
Column mapping@map("snake_case")

3. Setting Field Types

Scalar TypeJS/TS TypeDefault DB Type (PG)
Stringstringtext
Booleanbooleanboolean
Intnumberinteger
BigIntbigintbigint
Floatnumberdouble precision
DecimalDecimal.jsdecimal(65,30)
DateTimeDatetimestamp(3)
JsonJsonValuejsonb
BytesUint8Arraybytea

4. Using Type Modifiers

ModifierSyntaxMeaning
OptionalString?Nullable column
ListString[]Array (PG/Mongo only)
RequiredStringNOT NULL (default)

5. Adding Default Values

DefaultExample
Literal@default("active")
now()DateTime current timestamp
autoincrement()Int sequence
uuid() / uuid(7)UUID v4 / v7 string
cuid() / cuid(2)CUID identifier
nanoid(n)NanoID string
dbgenerated("expr")Raw SQL default

6. Setting Auto-Increment Fields

id Int @id @default(autoincrement())
NoteDetail
PGGenerates a SERIAL/IDENTITY column
MongoDBNot supported — use ObjectId
CockroachDBPrefer sequence()

7. Using UUID Generation

VariantBehavior
uuid()UUID v4 (random)
uuid(7) NEWUUID v7 (time-ordered, index-friendly)
Native type@db.Uuid on PostgreSQL

8. Using CUID Generation

VariantDescription
cuid()CUID v1 (25 chars)
cuid(2)CUID2: shorter, secure, URL-safe
Use caseDistributed-safe collision-resistant IDs

9. Creating Unique Fields

ApproachSyntax
Single fieldemail String @unique
Composite@@unique([firstName, lastName])
Named@@unique([a,b], name: "ab_uq")

10. Adding Field Documentation

model User {
  /// Primary email used for login
  email String @unique
}
CommentSurfaces In
///Generated client JSDoc, Prisma Studio tooltips
//Schema only