Skip to main content

Overview

The Prisma schema file (short: schema file, Prisma schema or schema) is the main configuration file for your Prisma ORM setup. It is typically called schema.prisma and consists of the following parts:

  • Data sources: Specify the details of the data sources Prisma ORM should connect to (e.g. a PostgreSQL database)
  • Generators: Specifies what clients should be generated based on the data model (e.g. Prisma Client)
  • Data model definition: Specifies your application models (the shape of the data per data source) and their relations

See the Prisma schema API reference for detailed information about each section of the schema.

Whenever a prisma command is invoked, the CLI typically reads some information from the schema file, e.g.:

  • prisma generate: Reads all above mentioned information from the Prisma schema to generate the correct data source client code (e.g. Prisma Client).
  • prisma migrate dev: Reads the data sources and data model definition to create a new migration.

You can also use environment variables inside the schema file to provide configuration options when a CLI command is invoked.

Example

The following is an example of a Prisma schema file that specifies:

  • A data source (PostgreSQL or MongoDB)
  • A generator (Prisma Client)
  • A data model definition with two models (with one relation) and one enum
  • Several native data type attributes (@db.VarChar(255), @db.ObjectId)
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

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

model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
role Role @default(USER)
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean @default(false)
title String @db.VarChar(255)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}

enum Role {
USER
ADMIN
}

Naming

The default name for the schema file is schema.prisma. When your schema file is named like this, the Prisma CLI will detect it automatically in the directory where you invoke the CLI command (or any of its subdirectories).

If the file is named differently, you can provide the --schema argument to the Prisma CLI with the path to the schema file, e.g.:

prisma generate --schema ./database/myschema.prisma

Syntax

The schema file is written in Prisma Schema Language (PSL).

VS Code

Syntax highlighting for PSL is available via a VS Code extension (which also lets you auto-format the contents of your Prisma schema and indicates syntax errors with red squiggly lines). Learn more about setting up Prisma ORM in your editor.

GitHub

PSL code snippets on GitHub can be rendered with syntax highlighting as well by using the .prisma file extension or annotating fenced code blocks in Markdown with prisma:

```prisma
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
}
```

Prisma schema file location

The Prisma CLI looks for the Prisma schema file in the following locations, in the following order:

  1. The location specified by the --schema flag, which is available when you introspect, generate, migrate, and studio:

    prisma generate --schema=./alternative/schema.prisma
  2. The location specified in the package.json file (version 2.7.0 and later):

    "prisma": {
    "schema": "db/schema.prisma"
    }
  3. Default locations:

    • ./prisma/schema.prisma
    • ./schema.prisma

The Prisma CLI outputs the path of the schema file that will be used. The following example shows the terminal output for prisma db pull:

Environment variables loaded from .env
|Prisma Schema loaded from prisma/schema.prisma

Introspecting based on datasource defined in prisma/schema.prisma …

✔ Introspected 4 models and wrote them into prisma/schema.prisma in 239ms

Run prisma generate to generate Prisma Client.

Accessing environment variables from the schema

You can use environment variables to provide configuration options when a CLI command is invoked, or a Prisma Client query is run.

Hardcoding URLs directly in your schema is possible but is discouraged because it poses a security risk. Using environment variables in the schema allows you to keep secrets out of the schema file which in turn improves the portability of the schema by allowing you to use it in different environments.

Environment variables can be accessed using the env() function:

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

You can use the env() function in the following places:

  • A datasource url
  • Generator binary targets

See Environment variables for more information about how to use an .env file during development.

Comments

There are two types of comments that are supported in the schema file:

  • // comment: This comment is for the reader's clarity and is not present in the abstract syntax tree (AST) of the schema file.
  • /// comment: These comments will show up in the abstract syntax tree (AST) of the schema file as descriptions to AST nodes. Tools can then use these comments to provide additional information. All comments are attached to the next available node - free-floating comments are not supported and are not included in the AST.

Here are some different examples:

/// This comment will get attached to the `User` node in the AST
model User {
/// This comment will get attached to the `id` node in the AST
id Int @default(autoincrement())
// This comment is just for you
weight Float /// This comment gets attached to the `weight` node
}

// This comment is just for you. It will not
// show up in the AST.

/// This comment will get attached to the
/// Customer node.
model Customer {}

Auto formatting

Prisma ORM supports formatting .prisma files automatically. There are two ways to format .prisma files:

There are no configuration options - formatting rules are fixed (similar to Golang's gofmt but unlike Javascript's prettier):

Formatting rules

Configuration blocks are aligned by their = sign.

block _ {
key = "value"
key2 = 1
long_key = true
}

A newline resets block alignment:

block _ {
key = "value"
key2 = 1
key10 = true

long_key = true
long_key_2 = true
}

Field definitions are aligned into columns separated by 2 or more spaces

block _ {
id String @id
first_name LongNumeric @default
}

Multiline field attributes are properly aligned with the rest of the field attributes

block _ {
id String @id
@default
first_name LongNumeric @default
}

A newline resets formatting rules:

block _ {
id String @id
@default

first_name LongNumeric @default
}

Block attributes are sorted to the end of the block

block _ {
key = "value"

@@attribute
}