apps/docs/content/docs/orm/prisma-client/type-safety/prisma-type-system.mdx
This guide introduces Prisma ORM's type system and explains how to introspect existing native types in your database, and how to use types when you apply schema changes to your database with Prisma Migrate or db push.
Prisma ORM uses types to define the kind of data that a field can hold. To make it easy to get started, Prisma ORM provides a small number of core scalar types that should cover most default use cases. For example, take the following blog post model:
datasource db {
provider = "postgresql"
}
model Post {
id Int @id
title String
createdAt DateTime
}
The title field of the Post model uses the String scalar type, while the createdAt field uses the DateTime scalar type.
Databases also have their own type system, which defines the type of value that a column can hold. Most databases provide a large number of data types to allow fine-grained control over exactly what a column can store. For example, a database might provide inbuilt support for multiple sizes of integers, or for XML data. The names of these types vary between databases. For example, in PostgreSQL the column type for booleans is boolean, whereas in MySQL the tinyint(1) type is typically used.
In the blog post example above, we are using the PostgreSQL connector. This is specified in the datasource block of the Prisma schema.
To allow you to get started with our core scalar types, Prisma ORM provides default type mappings that map each scalar type to a default type in the underlying database. For example:
String type gets mapped to PostgreSQL's text type and MySQL's varchar typeDateTime type gets mapped to PostgreSQL's timestamp(3) type and SQL Server's datetime2 typeSee Prisma ORM's database connector pages for the default type mappings for a given database. For example, this table gives the default type mappings for PostgreSQL.
To see the default type mappings for all databases for a specific given Prisma ORM type, see the model field scalar types section of the Prisma schema reference. For example, this table gives the default type mappings for the Float scalar type.
Sometimes you may need to use a more specific database type that is not one of the default type mappings for your Prisma ORM type. For this purpose, Prisma ORM provides native type attributes to refine the core scalar types. For example, in the createdAt field of your Post model above you may want to use a date-only column in your underlying PostgreSQL database, by using the date type instead of the default type mapping of timestamp(3). To do this, add a @db.Date native type attribute to the createdAt field:
model Post {
id Int @id
title String
createdAt DateTime @db.Date
}
Native type mappings allow you to express all the types in your database. However, you do not need to use them if the Prisma ORM defaults satisfy your needs. This leads to a shorter, more readable Prisma schema for common use cases.
When you introspect an existing database, Prisma ORM will take the database type of each table column and represent it in your Prisma schema using the correct Prisma ORM type for the corresponding model field. If the database type is not the default database type for that Prisma ORM scalar type, Prisma ORM will also add a native type attribute.
As an example, take a User table in a PostgreSQL database, with:
id column with a data type of serialname column with a data type of textisActive column with a data type of booleanYou can create this with the following SQL command:
CREATE TABLE "public"."User" (
id serial PRIMARY KEY NOT NULL,
name text NOT NULL,
"isActive" boolean NOT NULL
);
Introspect your database with the following command run from the root directory of your project:
npx prisma db pull
You will get the following Prisma schema:
model User {
id Int @id @default(autoincrement())
name String
isActive Boolean
}
The id, name and isActive columns in the database are mapped respectively to the Int, String and Boolean Prisma ORM types. The database types are the default database types for these Prisma ORM types, so Prisma ORM does not add any native type attributes.
Now add a createdAt column to your database with a data type of date by running the following SQL command:
ALTER TABLE "public"."User"
ADD COLUMN "createdAt" date NOT NULL;
Introspect your database again:
npx prisma db pull
Your Prisma schema now includes the new createdAt field with a Prisma ORM type of DateTime. The createdAt field also has a @db.Date native type attribute, because PostgreSQL's date is not the default type for the DateTime type:
model User {
id Int @id @default(autoincrement())
name String
isActive Boolean
createdAt DateTime @db.Date // [!code ++]
}
When you apply schema changes to your database using Prisma Migrate or db push, Prisma ORM will use both the Prisma ORM scalar type of each field and any native attribute it has to determine the correct database type for the corresponding column in the database.
As an example, create a Prisma schema with the following Post model:
model Post {
id Int @id
title String
createdAt DateTime
updatedAt DateTime @db.Date
}
This Post model has:
id field with a Prisma ORM type of Inttitle field with a Prisma ORM type of StringcreatedAt field with a Prisma ORM type of DateTimeupdatedAt field with a Prisma ORM type of DateTime and a @db.Date native type attributeNow apply these changes to an empty PostgreSQL database with the following command, run from the root directory of your project:
npx prisma db push
You will see that the database has a newly created Post table, with:
id column with a database type of integertitle column with a database type of textcreatedAt column with a database type of timestamp(3)updatedAt column with a database type of dateNotice that the @db.Date native type attribute modifies the database type of the updatedAt column to date, rather than the default of timestamp(3).
For further reference information on using Prisma ORM's type system, see the following resources:
String Prisma ORM type is here.