Sikau

Prisma · Day 2 · about 30 min

Models and fields

Types, ids, defaults, unique and optional fields.

The schema is the source of truth

Each model in schema.prisma becomes a table; each field a column. Attributes like @id, @default and @unique become primary keys, defaults and constraints — and the generated TypeScript types follow automatically.

Field types and attributes

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

enum Role {
  CUSTOMER
  STAFF
  ADMIN
}

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String?
  role      Role     @default(CUSTOMER)
  points    Int      @default(0)
  bio       String?  @map("biography")
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([name])
}

After changing the schema: npx prisma migrate dev --name add-points creates a migration file and updates the database.

The rest of this lesson

  • Mapping to existing namesexplain
  • Choosing field typesexplain
  • Required?predict
  • Attribute to meaningmatch
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 15 lessons of Prisma