Back to roadmaps prisma Course

Prisma Models and Field Definitions

Models in the Prisma schema represent the tables in your database. They determine the shape of your data records and form the foundation of the auto-generated Prisma Client API.


1. Basic Model Declaration Syntax

Here is an example of a model definition in a Prisma schema:

model Product {
  id          Int      @id @default(autoincrement())
  sku         String   @unique
  name        String
  description String?
  price       Decimal  @default(0.00)
  createdAt   DateTime @default(now())
}

Every field has a name, a type, and optional attributes.


2. Core Scalar Field Types

Prisma maps native database column types to standard schema scalar types:

  • String: For textual values (maps to text or varchar).
  • Int: For integer numbers.
  • Float: For decimal numbers.
  • Decimal: For high-precision currency values.
  • Boolean: For true/false flags.
  • DateTime: For timestamps.
  • Json: For unstructured nested data payloads (supported in PostgreSQL and MySQL).

3. Type Modifiers

Type modifiers customize the behavior of a field:

  • Optional Fields (?): Appending a question mark makes a field nullable in the database (for example, description String?).
  • List Fields ([]): Represents arrays of scalars (for example, tags String[]).

4. Common Field Attributes

Attributes customize column settings in the database:

  • @id: Marks the field as the primary key.
  • @default(val): Sets a default value for the column. Common helpers include:
    • autoincrement(): Automatically increments integer primary keys.
    • uuid(): Generates unique 36-character string IDs.
    • now(): Saves the current timestamp when a record is created.
  • @unique: Ensures all values in the column are unique.
  • @updatedAt: Automatically updates the timestamp value when the record is modified.
Published on Last updated: