CodingNic

Defining Models and Migrations

Fields, Types, and Attributes

Defining Models and Migrations 10 min read

Fields, Types, and Attributes

Objectives

By the end of this lesson, you should be able to:

  • Use Prisma’s core scalar types
  • Mark a field optional, unique, or with a default value
  • Read a complete model and explain every field in it

💡 Why this matters: Module 3 introduced a bare-minimum model. Real tables need optional fields, unique constraints, timestamps, and sensible defaults, this lesson covers the attributes that express all of that directly in the schema.

⚠️ A note on verification: as throughout this module, the Prisma CLI can’t run inside this course’s own sandboxed tooling. The schema syntax below is Prisma’s stable, current, documented syntax. Try it against your own project, following Module 3’s setup.

Core Scalar Types

Prisma type Maps to (PostgreSQL) Example
String TEXT name String
Int INTEGER grade Int
Boolean BOOLEAN available Boolean
DateTime TIMESTAMP createdAt DateTime
Float DOUBLE PRECISION price Float

Optional Fields

A ? after the type marks a field optional, allowed to be null:

text
model Student {
  id     Int     @id @default(autoincrement())
  name   String
  grade  Int
  email  String?
}

email String? means a student can be created without an email. name String (no ?) is required, Prisma refuses to create a Student without one.

Unique Fields

@unique enforces that no two rows can share the same value:

text
model Member {
  id    Int    @id @default(autoincrement())
  name  String
  email String @unique
}

Attempting to create a second Member with an email already in use fails, the same guarantee UNIQUE gives in raw SQL.

Default Values

@default(...) sets a value automatically when one isn’t provided:

text
model Book {
  id        Int      @id @default(autoincrement())
  title     String
  available Boolean  @default(true)
  addedAt   DateTime @default(now())
}
  • @default(autoincrement()), already seen on every id field, an auto-incrementing integer.
  • @default(true), a fixed default value.
  • @default(now()), the current timestamp at the moment the row is created.

A Complete Model

text
model Student {
  id        Int      @id @default(autoincrement())
  name      String
  grade     Int
  email     String?  @unique
  enrolledAt DateTime @default(now())
}

Reading it field by field: an auto-incrementing integer primary key, a required name, a required grade, an optional but unique email (unique still applies even when the value is null-able), and an enrollment timestamp that defaults to the moment the row is created.

Try It

  1. Add an optional phone field (String?) to a Student model.
  2. Add a @unique email field to the same model.
  3. Add a createdAt field with @default(now()).
  4. Write out, in words, what happens if code tries to create two students with the same email.

Recap

  • Prisma’s core scalar types (String, Int, Boolean, DateTime, Float) map directly to familiar SQL column types.
  • ? marks a field optional, @unique enforces uniqueness, @default(...) sets an automatic value, including now() for timestamps.
  • These attributes are read directly off the schema, no separate configuration step needed.

Next lesson: turning a schema like this into a real table, with your first migration.