Skip to content

Prisma Schema

Prisma schema file is the main configuration file for Prisma. It is usually named schema.prisma and consists of the following sections:

Data source

Data sources determine how Prisma connects to your database and are represented by a datasource block in your Prisma schema file. Here's an example of a data source using postgresql as the database and containing a hard-coded connection string:

prisma
generator client {
  provider = "dart run orm"
}

datasource db { 
  provider = "postgresql"
  url      = "postgresql://johndoe:mypassword@localhost:5432/mydb?schema=public"
} 

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

Generators

Prisma schema can have one or more generators, represented by a generator block:

prisma
generator client { 
  provider = "dart run orm"
} 

datasource db {
  provider = "postgresql"
  url      = "postgresql://johndoe:mypassword@localhost:5432/mydb?schema=public"
}

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

TIP

About more generators, see 👉 Generators

Prisma Dart client: dart run orm

You need to specify the provider as dart run orm in the generator when you decide to use Prisma in your Dart project, so that Prisma can generate Dart code for you.

prisma
generator client {
  provider = "dart run orm"
}

Output directory

  • output: The directory where the generated Dart code will be written to. Defaults to ./generated_dart_client/.

Relative paths are resolved relative to the directory of the Prisma schema file.

Change the output directory:

prisma
generator client {
  provider = "dart run orm"
  output   = "../lib/src/generated/prisma_client"
}

Data model

Data models define the structure of your database tables. They are made up of model/view/enum blocks.

TIP

The following sections only make a simple introduction. For more content derived from the data model, please refer to 👉 Data Model

Model

Models are represented by a model block in your Prisma schema file. Here's an example of a User model:

prisma

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

view UserDetail {
  id    Int
  name  String?
  postsCount Int
}

enum Role {
  USER
  ADMIN
}

View

Views are represented by a view block in your Prisma schema file. Here's an example of a UserDetail view:

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

view UserDetail { 
  id    Int
  name  String?
  postsCount Int
} 

enum Role {
  USER
  ADMIN
}

DANGER

view is currently a preview feature and should not be used in production. Prisma Dart Client cannot guarantee the correctness of view.

About more views, see 👉 Views

Enum

Enums are represented by an enum block in your Prisma schema file. Here's an example of a Role enum:

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

view UserDetail {
  id    Int
  name  String?
  postsCount Int
}

enum Role { 
  USER 
  ADMIN 
} 

More

About more data sources, see 👉 Prisma Data Source official documentation.

Released under the BSD-3-Clause License