Node.js, Prisma & PostgreSQL

Lesson 3 of 12

Defining Your Prisma Schema

prisma/schema.prisma has three parts: a generator, a datasource, and one or more models.

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

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

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}

Reading a model

  • Each field has a name, a type (Int, String, Boolean, DateTime, ...), and optional attributes.
  • @id marks the primary key.
  • @default(autoincrement()) lets PostgreSQL assign the next integer automatically, exactly like SERIAL in raw SQL.
  • @unique adds a unique constraint, no two users can share an email.
  • A trailing ? (like String?) makes a field optional, it maps to a nullable column.

Every model becomes a table, every field becomes a column, this is the same shape of data you already modeled in the SQL Fundamentals course, just described declaratively instead of with CREATE TABLE.

📝 Schema Quiz

Passing score: 70%
  1. 1.Which attribute marks a field as the primary key?

  2. 2.A field type written with a trailing ____ mark, like String?, is optional in the database.

  3. 3.Every model block in schema.prisma becomes a table in the database.