Extended native types support

Extended native database type support is a Preview feature in 2.11.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
  • 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)
}

In a future release, Prisma Migrate support for type attributes will give you greater control over exactly which types are created in the underlying database.

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

Note: db push and Prisma Migrate are not yet supported, which means that you cannot add the type attributes to the schema yourself.

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

Currently, the main benefit of native database type attributes is an enriched schema when you introspect - you can see if String is varchar(200) or just text.

In future, native database type attributes will allow you finer control over exactly what type is created in the database when you migrate.

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 Primsa 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!"])
}
}
})

Limitations

Prisma Migrate not yet supported

Prisma Migrate does not yet support native database type attributes. If you try to prisma migrate or prisma db push, you will see the following error:

$Response "Some of the requested Preview features are not yet allowed in migration engine. Please remove them from your data model before using migrations. (blocked: `nativeTypes`)"

db push not yet supported

db push uses the Migration Engine, which does not support native database type attributes yet.

Edit this page on GitHub