Extended native types support

Extended native database type support is a Preview feature in 2.11.0+, and is supported by Prisma Migrate in 2.15.0+. This Preview includes the following features:

  • Introspection adds type attributes to each field that enrich your schema with information about the underlying native database type
  • Prisma Migrate uses type attributes to infer which specific native type to create in the database - for example, @VarChar(90) or @VarChar(200)
  • New Prisma types

In the following example, the String fields map to a varchar with different character limits in the underlying database:

model Post {
authorId Int? @db.Int
content String? @db.VarChar(1000)
id Int @id @default(autoincrement()) @db.Int
published Boolean @default(false) @db.TinyInt
title String @db.VarChar(191)
}

See also:

Native database type attributes

When you enable the nativeTypes Preview feature, introspection adds a native database type attribute that describes the underlying database type:

model Post {
id Int @id
title String @db.VarChar(200)
tagline String @db.Char(50)
}

Each Prisma type can map to multiple native database types. Native database type attributes are:

  • Specific to the underlying provider - for example, PostgreSQL uses @db.Boolean for Boolean whereas MySQL uses @db.TinyInt
  • Written in PascalCase (for example, VarChar or Text)
  • Prefixed by @db, where db is the name of the datasource block in your schema

Type attributes give you:

  • Exact control over what native type [Prisma Migrate creates in the database - for example, a String can be @db.VarChar(200) or @db.Char(50)
  • An enriched schema when you introspect - you can see if String is varchar(200) or just text.

Enable native database type attributes

To enable native database type attributes:

  1. Update the generator block in your schema as shown:

    prisma/schema.prisma
    1generator client {
    2 provider = "prisma-client-js"
    3 previewFeatures = ["nativeTypes"]
    4}
  2. Introspect your database to populate the schema with type attributes:

    $npx prisma introspect
  3. Generate Prisma Client:

    $npx prisma generate

Additional Prisma types

The type attribute Preview feature includes the following new Prisma types:

Working with new types in Prisma Client

The following model uses the three new Prisma types:

model sample {
bigInt BigInt? @db.BigInt
byteA Bytes? @db.ByteA
decimal Decimal? @db.Numeric
id Int @id @default(autoincrement()) @db.Integer
}

The following example demonstrates how to create and filter on sample records with Prisma Client:

// Create record
const newTypes = await prisma.sample.create({
data:{
bigInt: BigInt(534543543534),
decimal: new Decimal(24.454545),
byteA: Buffer.from(["Hello", "this is a string", 1, 9, 4])
}
});
// Filter records
const newTypesFilter = await prisma.dates.findMany({
where: {
bigInt: {
gt: BigInt(4594375943753)
},
byteA: {
not: Buffer.from(["Hey!"])
}
}
})
Edit this page on GitHub