Prisma schema reference
datasource
Defines a data source in the Prisma schema.
Fields
A datasource
block accepts the following fields:
Name | Required | Type | Description |
---|---|---|---|
provider | Yes | String (postgresql , mysql , sqlite , sqlserver , mongodb , cockroachdb ) | Describes which data source connectors to use. |
url | Yes | String (URL) | Connection URL including authentication info. Most connectors use the syntax provided by the database. |
shadowDatabaseUrl | No | String (URL) | Connection URL to the shadow database used by Prisma Migrate. Allows you to use a cloud-hosted database as the shadow database. |
directUrl | No | String (URL) | Connection URL for direct connection to the database. If you use a connection pooler URL in the url argument (for example, if you use Prisma Accelerate or pgBouncer), Prisma CLI commands that require a direct connection to the database use the URL in the directUrl argument. The directUrl property is supported by Prisma Studio from version 5.1.0 upwards. The directUrl property is not needed when using Prisma Postgres database. |
relationMode | No | String (foreignKeys , prisma ) | Sets whether referential integrity is enforced by foreign keys in the database or emulated in the Prisma Client. In preview in versions 3.1.1 and later. The field is named relationMode in versions 4.5.0 and later, and was previously named referentialIntegrity . |
extensions | No | List of strings (PostgreSQL extension names) | Allows you to represent PostgreSQL extensions in your schema. Available in preview for PostgreSQL only in Prisma ORM versions 4.5.0 and later. |
The following providers are available:
Remarks
- You can only have one
datasource
block in a schema. datasource db
is convention - however, you can give your data source any name - for example,datasource mysql
ordatasource data
.
Examples
Specify a PostgreSQL data source
In this example, the target database is available with the following credentials:
- User:
johndoe
- Password:
mypassword
- Host:
localhost
- Port:
5432
- Database name:
mydb
- Schema name:
public
datasource db {
provider = "postgresql"
url = "postgresql://johndoe:mypassword@localhost:5432/mydb?schema=public"
}
Learn more about PostgreSQL connection strings here.
Specify a PostgreSQL data source via an environment variable
In this example, the target database is available with the following credentials:
- User:
johndoe
- Password:
mypassword
- Host:
localhost
- Port:
5432
- Database name:
mydb
- Schema name:
public
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
When running a Prisma CLI command that needs the database connection URL (e.g. prisma generate
), you need to make sure that the DATABASE_URL
environment variable is set.
One way to do so is by creating a .env
file with the following contents. Note that the file must be in the same directory as your schema.prisma
file to automatically picked up the Prisma CLI.
DATABASE_URL=postgresql://johndoe:mypassword@localhost:5432/mydb?schema=public
Specify a MySQL data source
In this example, the target database is available with the following credentials:
- User:
johndoe
- Password:
mypassword
- Host:
localhost
- Port:
3306
- Database name:
mydb
datasource db {
provider = "mysql"
url = "mysql://johndoe:mypassword@localhost:3306/mydb"
}
Learn more about MySQL connection strings here.
Specify a MongoDB data source
- User:
root
- Password:
password
- Host:
cluster1.test1.mongodb.net
- Port: N/A
- Database name:
testing
datasource db {
provider = "mongodb"
url = "mongodb+srv://root:password@cluster1.test1.mongodb.net/testing?retryWrites=true&w=majority"
}
Learn more about MongoDB connection strings here.
Specify a SQLite data source
In this example, the target database is located in a file called dev.db
:
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
Learn more about SQLite connection strings here.
Specify a CockroachDB data source
In this example, the target database is available with the following credentials:
- User:
johndoe
- Password:
mypassword
- Host:
localhost
- Port:
26257
- Database name:
mydb
- Schema name:
public
datasource db {
provider = "cockroachdb"
url = "postgresql://johndoe:mypassword@localhost:26257/mydb?schema=public"
}
The format for connection strings is the same as for PostgreSQL. Learn more about PostgreSQL connection strings here.
generator
Defines a generator in the Prisma schema.
Fields
A generator
block accepts the following fields:
Name | Required | Type | Description |
---|---|---|---|
provider | Yes | String (file path) or Enum (prisma-client-js ) | Describes which generator to use. This can point to a file that implements a generator or specify a built-in generator directly. |
output | No | String (file path) | Determines the location for the generated client, learn more. Default: node_modules/.prisma/client |
previewFeatures | No | List of Enums | Use intellisense to see list of currently available Preview features (Ctrl+Space in Visual Studio Code) Default: none |
engineType | No | Enum (library or binary ) | Defines the query engine type to download and use. Default: library |
binaryTargets | No | List of Enums (see below) | Specify the OS on which the Prisma Client will run to ensure compatibility of the query engine. Default: native |
binaryTargets
options
The following tables list all supported operating systems with the name of platform to specify in binaryTargets
.
Unless specified otherwise, the default supported CPU architecture is x86_64.
macOS
Build OS | Prisma engine build name |
---|---|
macOS Intel x86_64 | darwin |
macOS ARM64 | darwin-arm64 |
Windows
Build OS | Prisma engine build name |
---|---|
Windows | windows |
Linux (Alpine on x86_64 architectures)
Build OS | Prisma engine build name | OpenSSL |
---|---|---|
Alpine (3.17 and newer) | linux-musl-openssl-3.0.x * | 3.0.x |
Alpine (3.16 and older) | linux-musl | 1.1.x |
* Available in Prisma ORM versions 4.8.0 and later.
Linux (Alpine on ARM64 architectures)
Build OS | Prisma engine build name | OpenSSL |
---|---|---|
Alpine (3.17 and newer) | linux-musl-arm64-openssl-3.0.x * | 3.0.x |
Alpine (3.16 and older) | linux-musl-arm64-openssl-1.1.x * | 1.1.x |
* Available in Prisma ORM versions 4.10.0 and later.
Linux (Debian), x86_64
Build OS | Prisma engine build name | OpenSSL |
---|---|---|
Debian 8 (Jessie) | debian-openssl-1.0.x | 1.0.x |
Debian 9 (Stretch) | debian-openssl-1.1.x | 1.1.x |
Debian 10 (Buster) | debian-openssl-1.1.x | 1.1.x |
Debian 11 (Bullseye) | debian-openssl-1.1.x | 1.1.x |
Debian 12 (Bookworm) | debian-openssl-3.0.x | 3.0.x |
Linux (Ubuntu), x86_64
Build OS | Prisma engine build name | OpenSSL |
---|---|---|
Ubuntu 14.04 (trusty) | debian-openssl-1.0.x | 1.0.x |
Ubuntu 16.04 (xenial) | debian-openssl-1.0.x | 1.0.x |
Ubuntu 18.04 (bionic) | debian-openssl-1.1.x | 1.1.x |
Ubuntu 19.04 (disco) | debian-openssl-1.1.x | 1.1.x |
Ubuntu 20.04 (focal) | debian-openssl-1.1.x | 1.1.x |
Ubuntu 21.04 (hirsute) | debian-openssl-1.1.x | 1.1.x |
Ubuntu 22.04 (jammy) | debian-openssl-3.0.x | 3.0.x |
Ubuntu 23.04 (lunar) | debian-openssl-3.0.x | 3.0.x |
Linux (CentOS), x86_64
Build OS | Prisma engine build name | OpenSSL |
---|---|---|
CentOS 7 | rhel-openssl-1.0.x | 1.0.x |
CentOS 8 | rhel-openssl-1.1.x | 1.1.x |
Linux (Fedora), x86_64
Build OS | Prisma engine build name | OpenSSL |
---|---|---|
Fedora 28 | rhel-openssl-1.1.x | 1.1.x |
Fedora 29 | rhel-openssl-1.1.x | 1.1.x |
Fedora 30 | rhel-openssl-1.1.x | 1.1.x |
Fedora 36 | rhel-openssl-3.0.x | 3.0.x |
Fedora 37 | rhel-openssl-3.0.x | 3.0.x |
Fedora 38 | rhel-openssl-3.0.x | 3.0.x |
Linux (Linux Mint), x86_64
Build OS | Prisma engine build name | OpenSSL |
---|---|---|
Linux Mint 18 | debian-openssl-1.0.x | 1.0.x |
Linux Mint 19 | debian-openssl-1.1.x | 1.1.x |
Linux Mint 20 | debian-openssl-1.1.x | 1.1.x |
Linux Mint 21 | debian-openssl-3.0.x | 3.0.x |
Linux (Arch Linux), x86_64
Build OS | Prisma engine build name | OpenSSL |
---|---|---|
Arch Linux 2019.09.01 | debian-openssl-1.1.x | 1.1.x |
Arch Linux 2023.04.23 | debian-openssl-3.0.x | 3.0.x |
Linux ARM64 (all major distros but Alpine)
Build OS | Prisma engine build name | OpenSSL |
---|---|---|
Linux ARM64 glibc-based distro | linux-arm64-openssl-1.0.x | 1.0.x |
Linux ARM64 glibc-based distro | linux-arm64-openssl-1.1.x | 1.1.x |
Linux ARM64 glibc-based distro | linux-arm64-openssl-3.0.x | 3.0.x |
Examples
Specify the prisma-client-js
generator with the default output
, previewFeatures
, engineType
and binaryTargets
generator client {
provider = "prisma-client-js"
}
Note that the above generator
definition is equivalent to the following because it uses the default values for output
, engineType
and binaryTargets
(and implicitly previewFeatures
):
generator client {
provider = "prisma-client-js"
output = "node_modules/.prisma/client"
engineType = "library"
binaryTargets = ["native"]
}
Specify a custom output
location for Prisma Client
This example shows how to define a custom output
location of the generated asset to override the default one.
generator client {
provider = "prisma-client-js"
output = "../src/generated/client"
}
Specify custom binaryTargets
to ensure compatibility with the OS
This example shows how to configure Prisma Client to run on Ubuntu 19.04 (disco)
based on the table above.
generator client {
provider = "prisma-client-js"
binaryTargets = ["debian-openssl-1.1.x"]
}
Specify a provider
pointing to some custom generator implementation
This example shows how to use a custom generator that's located in a directory called my-generator
.
generator client {
provider = "./my-generator"
}
model
Defines a Prisma model .
Remarks
- Every record of a model must be uniquely identifiable. You must define at least one of the following attributes per model:
Naming conventions
- Model names must adhere to the following regular expression:
[A-Za-z][A-Za-z0-9_]*
- Model names must start with a letter and are typically spelled in PascalCase
- Model names should use the singular form (for example,
User
instead ofuser
,users
orUsers
) - Prisma ORM has a number of reserved words that are being used by Prisma ORM internally and therefore cannot be used as a model name. You can find the reserved words here and here.
Note: You can use the
@@map
attribute to map a model (for example,User
) to a table with a different name that does not match model naming conventions (for example,users
).
Order of fields
- In version 2.3.0 and later, introspection lists model fields in the same order as the corresponding columns in the database. Relation fields are listed after scalar fields.
Examples
A model named User
with two scalar fields
- Relational databases
- MongoDB
model User {
email String @unique // `email` can not be optional because it's the only unique field on the model
name String?
}
model User {
id String @default(auto()) @map("_id") @db.ObjectId
email String @unique
name String?
}
model
fields
Fields are properties of models.
Remarks
Naming conventions
- Must start with a letter
- Typically spelled in camelCase
- Must adhere to the following regular expression:
[A-Za-z][A-Za-z0-9_]*
Note: You can use the
@map
attribute to map a field name to a column with a different name that does not match field naming conventions: e.g.myField @map("my_field")
.
model
field scalar types
The data source connector determines what native database type each of Prisma ORM scalar type maps to. Similarly, the generator determines what type in the target programming language each of these types map to.
Prisma models also have model field types that define relations between models.
String
Variable length text.
Default type mappings
Connector | Default mapping |
---|---|
PostgreSQL | text |
SQL Server | nvarchar(1000) |
MySQL | varchar(191) |
MongoDB | String |
SQLite | TEXT |
CockroachDB | STRING |
PostgreSQL
Native database type | Native database type attribute | Notes |
---|---|---|
text | @db.Text | |
char(x) | @db.Char(x) | |
varchar(x) | @db.VarChar(x) | |
bit(x) | @db.Bit(x) | |
varbit | @db.VarBit | |
uuid | @db.Uuid | |
xml | @db.Xml | |
inet | @db.Inet | |
citext | @db.Citext | Only available if Citext extension is enabled. |
MySQL
Native database type | Native database type attribute |
---|---|
VARCHAR(x) | @db.VarChar(x) |
TEXT | @db.Text |
CHAR(x) | @db.Char(x) |
TINYTEXT | @db.TinyText |
MEDIUMTEXT | @db.MediumText |
LONGTEXT | @db.LongText |
You can use Prisma Migrate to map @db.Bit(1)
to String
:
model Model {
/* ... */
myField String @db.Bit(1)
}
MongoDB
String
Native database type attribute | Notes |
---|---|
@db.String | |
@db.ObjectId | Required if the underlying BSON type is OBJECT_ID (ID fields, relation scalars) |
Microsoft SQL Server
Native database type | Native database type attribute |
---|---|
char(x) | @db.Char(x) |
nchar(x) | @db.NChar(x) |
varchar(x) | @db.VarChar(x) |
nvarchar(x) | @db.NVarChar(x) |
text | @db.Text |
ntext | @db.NText |
xml | @db.Xml |
uniqueidentifier | @db.UniqueIdentifier |
SQLite
TEXT
CockroachDB
Native database type | Native database type attribute | Notes |
---|---|---|
STRING(x) | TEXT(x) | VARCHAR(x) | @db.String(x) | |
CHAR(x) | @db.Char(x) | |
"char" | @db.CatalogSingleChar | |
BIT(x) | @db.Bit(x) | |
VARBIT | @db.VarBit | |
UUID | @db.Uuid | |
INET | @db.Inet |
Note that the xml
and citext
types supported in PostgreSQL are not currently supported in CockroachDB.
Clients
Prisma Client JS |
---|
string |
Boolean
True or false value.
Default type mappings
Connector | Default mapping |
---|---|
PostgreSQL | boolean |
SQL Server | bit |
MySQL | TINYINT(1) |
MongoDB | Bool |
SQLite | INTEGER |
CockroachDB | BOOL |
PostgreSQL
Native database types | Native database type attribute | Notes |
---|---|---|
boolean | @db.Boolean |
MySQL
Native database types | Native database type attribute | Notes |
---|---|---|
TINYINT(1) | @db.TinyInt(1) | TINYINT maps to Int if the max length is greater than 1 (for example, TINYINT(2) ) or the default value is anything other than 1 , 0 , or NULL |
BIT(1) | @db.Bit |
MongoDB
Bool
Microsoft SQL Server
Native database types | Native database type attribute | Notes |
---|---|---|
bit | @db.Bit |
SQLite
INTEGER
CockroachDB
Native database types | Native database type attribute | Notes |
---|---|---|
BOOL | @db.Bool |
Clients
Prisma Client JS |
---|
boolean |
Int
Default type mappings
Connector | Default mapping |
---|---|
PostgreSQL | integer |
SQL Server | int |
MySQL | INT |
MongoDB | Int |
SQLite | INTEGER |
CockroachDB | INT |
PostgreSQL
Native database types | Native database type attribute | Notes |
---|---|---|
integer | int , int4 | @db.Integer | |
smallint | int2 | @db.SmallInt | |
smallserial | serial2 | @db.SmallInt @default(autoincrement()) | |
serial | serial4 | @db.Int @default(autoincrement()) | |
oid | @db.Oid |
MySQL
Native database types | Native database type attribute | Notes |
---|---|---|
INT | @db.Int | |
INT UNSIGNED | @db.UnsignedInt | |
SMALLINT | @db.SmallInt | |
SMALLINT UNSIGNED | @db.UnsignedSmallInt | |
MEDIUMINT | @db.MediumInt | |
MEDIUMINT UNSIGNED | @db.UnsignedMediumInt | |
TINYINT | @db.TinyInt | TINYINT maps to Int if the max length is greater than 1 (for example, TINYINT(2) ) or the default value is anything other than 1 , 0 , or NULL . TINYINT(1) maps to Boolean . |
TINYINT UNSIGNED | @db.UnsignedTinyInt | TINYINT(1) UNSIGNED maps to Int , not Boolean |
YEAR | @db.Year |
MongoDB
Int
Native database type attribute | Notes |
---|---|
@db.Int | |
@db.Long |
Microsoft SQL Server
Native database types | Native database type attribute | Notes |
---|---|---|
int | @db.Int | |
smallint | @db.SmallInt | |
tinyint | @db.TinyInt | |
bit | @db.Bit |
SQLite
INTEGER
CockroachDB
Native database types | Native database type attribute | Notes |
---|---|---|
INTEGER | INT | INT8 | @db.Int8 | Note that this differs from PostgreSQL, where integer and int are aliases for int4 and map to @db.Integer |
INT4 | @db.Int4 | |
INT2 | SMALLINT | @db.Int2 | |
SMALLSERIAL | SERIAL2 | @db.Int2 @default(autoincrement()) | |
SERIAL | SERIAL4 | @db.Int4 @default(autoincrement()) | |
SERIAL8 | BIGSERIAL | @db.Int8 @default(autoincrement()) |
Clients
Prisma Client JS |
---|
number |
BigInt
BigInt
is available in version 2.17.0 and later.
Default type mappings
Connector | Default mapping |
---|---|
PostgreSQL | bigint |
SQL Server | int |
MySQL | BIGINT |
MongoDB | Long |
SQLite | INTEGER |
CockroachDB | INTEGER |
PostgreSQL
Native database types | Native database type attribute | Notes |
---|---|---|
bigint | int8 | @db.BigInt | |
bigserial | serial8 | @db.BigInt @default(autoincrement()) |
MySQL
Native database types | Native database type attribute | Notes |
---|---|---|
BIGINT | @db.BigInt | |
SERIAL | @db.UnsignedBigInt @default(autoincrement()) |
MongoDB
Long
Microsoft SQL Server
Native database types | Native database type attribute | Notes |
---|---|---|
bigint | @db.BigInt |
SQLite
INTEGER
CockroachDB
Native database types | Native database type attribute | Notes |
---|---|---|
BIGINT | INT | INT8 | @db.Int8 | Note that this differs from PostgreSQL, where int is an alias for int4 |
bigserial | serial8 | @db.Int8 @default(autoincrement()) |
Clients
Client | Type | Description |
---|---|---|
Prisma Client JS | BigInt | See examples of working with BigInt |
Float
Floating point number.
Float
maps toDouble
in 2.17.0 and later - see release notes and Video: Changes to the default mapping of Float in Prisma ORM 2.17.0 for more information about this change.
Default type mappings
Connector | Default mapping |
---|---|
PostgreSQL | double precision |
SQL Server | float(53) |
MySQL | DOUBLE |
MongoDB | Double |
SQLite | REAL |
CockroachDB | DOUBLE PRECISION |
PostgreSQL
Native database types | Native database type attribute | Notes |
---|---|---|
double precision | @db.DoublePrecision | |
real | @db.Real |
MySQL
Native database types | Native database type attribute | Notes |
---|---|---|
FLOAT | @db.Float | |
DOUBLE | @db.Double |
MongoDB
Double
Microsoft SQL Server
Native database types | Native database type attribute |
---|---|
float | @db.Float |
money | @db.Money |
smallmoney | @db.SmallMoney |
real | @db.Real |
SQLite connector
REAL
CockroachDB
Native database types | Native database type attribute | Notes |
---|---|---|
DOUBLE PRECISION | FLOAT8 | @db.Float8 | |
REAL | FLOAT4 | FLOAT | @db.Float4 |
Clients
Prisma Client JS |
---|
number |
Decimal
Default type mappings
Connector | Default mapping |
---|---|
PostgreSQL | decimal(65,30) |
SQL Server | decimal(32,16) |
MySQL | DECIMAL(65,30) |
MongoDB | Not supported |
SQLite | DECIMAL |
CockroachDB | DECIMAL |
PostgreSQL
Native database types | Native database type attribute | Notes |
---|---|---|
decimal | numeric | @db.Decimal(p, s) † | |
money | @db.Money |
- †
p
(precision), the maximum total number of decimal digits to be stored.s
(scale), the number of decimal digits that are stored to the right of the decimal point.
MySQL
Native database types | Native database type attribute | Notes |
---|---|---|
DECIMAL | NUMERIC | @db.Decimal(p, s) † |
- †
p
(precision), the maximum total number of decimal digits to be stored.s
(scale), the number of decimal digits that are stored to the right of the decimal point.