rembrembdocs

Overview

Overview of Prisma Schema

The Prisma schema is the main method of configuration when using Prisma. It is typically called schema.prisma and contains your database connection and data model

The Prisma Schema (or schema for short) is the main method of configuration for your Prisma ORM setup. It consists of the following parts:

It is typically a single file called schema.prisma (or multiple files with .prisma file extension) that is stored in a defined but customizable location. You can also organize your Prisma schema in multiple files if you prefer that.

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, e.g.:

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

The following is an example of a Prisma Schema that specifies:

Prisma Schema files are written in Prisma Schema Language (PSL). See the data sources, generators, data model definition and of course Prisma Schema API reference pages for details and examples.

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?
}

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** 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" }


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

-   A datasource url
-   Generator binary targets

See [Environment variables](../../more/dev-environment/environment-variables/index.md) for more information about how to use an `.env` file during development.

There are three types of comments that are supported in Prisma Schema Language:

-   `// comment`: This comment is for the reader's clarity and is not present in the abstract syntax tree (AST) of the schema.
-   `/// comment`: These comments will show up in the abstract syntax tree (AST) of the schema 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](https://github.com/prisma/prisma/issues/3544) are not supported and are not included in the AST.
-   `/* block comment */`: These comments will show up in the abstract syntax tree, similarly to `///` comments.

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 { /**


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

-   Run the [`prisma format`](../../reference/prisma-cli-reference/index.md#format) command.
-   Install the [Prisma VS Code extension](https://marketplace.visualstudio.com/items?itemName=Prisma.prisma) and invoke the [VS Code format action](https://code.visualstudio.com/docs/editor/codebasics#_formatting) - manually or on save.

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

### [Formatting rules](#formatting-rules)

#### [Configuration blocks are aligned by their = sign](#configuration-blocks-are-aligned-by-theirsign)

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


#### [Field definitions are aligned into columns separated by 2 or more spaces](#field-definitions-are-aligned-into-columns-separated-by-2-or-more-spaces)

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


#### [Empty lines resets block alignment and formatting rules](#empty-lines-resets-block-alignment-and-formatting-rules)

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

long_key = true long_key_2 = true }

block _ { id String @id @default

first_name LongNumeric @default }


#### [Multiline field attributes are properly aligned with the rest of the field attributes](#multiline-field-attributes-are-properly-aligned-with-the-rest-of-the-field-attributes)

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


#### [Block attributes are sorted to the end of the block](#block-attributes-are-sorted-to-the-end-of-the-block)

block _ { key = "value"

@@attribute }