Repository navigation
Description
Problem
Today we support the following core data types across databases:
- String
- Boolean
- Int
- Float
- DateTime
- Json
While this is a useful set of data types for getting started, many databases offer a richer set of native database types. For example,
- Postgres offers data types like
numeric,date,varchar,smallint, andmoney. - MySQL offers data types like
blob,varbinary, andset.
These datatypes influence how the data is stored and the operations you can perform on that data. Prisma needs to understand these native database types better across it's tooling.
- Introspect: We understand these native database types, and map them to core data types. There's information loss here.
- Schema: We do not persist these native database types in the schema. For example, a Prisma
Intcould map to ainteger,smallintorbigint. - Client: We do not take advantage of the additional information native types provide. For example, our Typescript client could use the BigInt Javascript type for the database data type
bigint. - Migrate: We're only able to migrate a narrow set of native database types. For example, we can map
Inttointeger, but we cannot mapInttobigint. We also cannot currently generate text fields from the String type without more granular data types. - Studio: Database native types could influence formatting. For example, the
moneytype could be prefixed with$.
Solution
In the last days we looked into this and have reached internal consensus on a proposal.
The proposal is based on the mechanism that a user is decorating a field with an additional attribute that specifies the native data type in the underlying datasource. The attribute is scoped to the underlying datasource by prefixing it with {datasource_name}.{data_type_name}. The scoping is intended for future proofing the schema for support for multiple datasources within one schema.
datasource mydb {
provider = "postgres"
url = "..."
}
model Blog {
id Int @id
title String @mydb.VarChar(99)
someJson Json @mydb.JsonB
}Notes
- If the user does not specify a custom type this way the existing default type mapping kicks in.
- A user can easily see which data types are used in the client and
which one in the datasource because the combination is spelled out explicitly. - The name of the native type is the Pascal cased version of the original name in the docs of the database. E.g.
varchar(x)on Postgres becomes@myb.VarChar(x). - The user needs to know the compatibility of prisma and datasource types. E.g. a
Stringis compatible with@mydb.VarChar(x)and@mydb.Text. ButStringis not compatible with@mydb.XML. The parser will validate this and error for wrong combinations. The VS Code extension will be to assist with this through auto completion. - A native type always maps to exactly one Prisma data type.
Status
- Done/In Preview Add support for native types in the Schema.
- Done/In Preview Update Introspect to understand and generate Prisma Schemas with native types.
- Done/In Preview Add support for native types in Prisma Migrate
- Done/In Preview
Unsupportedtype: Ensure Prisma Migrate does not drop columns or tables with columns of unsupported types - these columns are also not exposed in Client [Native Types] Ensure that fields of unsupported types aren't dropped #3673 - Done/In Preview
dbgenerated(STRING): Make it possible to encode raw database defaults in the@defaultattribute of fields - Done/In Preview
@@ignoreattribute for models and@ignoreattribute for fields to be managed via Prisma Migrate and introspection but not surfaced in Prisma Client Schema notation to not surface specific models in the Prisma Client API #5217
Not planned yet - Done/In Preview Upgrading Client to take advantage of native types is still up for discussion.
- Done/In Preview Studio has basic support for native types
Related issues
- Native Types for SQL Server: Supporting native types on SQL Server #4077
Additional context
- There's a lot more discussion about native types in this issue.
- Request for binary: Support for binary type #1747
- Request for PostGIS: PostGIS/GIS support #2789
Activity
[-]Support Native Types[/-][+]Support Database Native Types[/+][-]Support Database Native Types[/-][+]Epic: Support Database Native Types[/+][-]Epic: Support Database Native Types[/-][+]Epic: Support Native Database Types[/+]Hebilicious commentedon Sep 3, 2020
The solution looks really clean !
Since the introspection will work before the migration, will it work with geometry columns (ie mapping geojson-like columns to json) ? While I understand the entire spatial database support is outside of the scope of this ticket, this would be nice to have.
Adding support for native types in Migrate will come later. => Any chance you could move this to the next steps ? Unplanned sounds a little bit scary. Imo this is one of the most valuable features to have when trying to push for prisma adoption in a project.
janpio commentedon Sep 3, 2020
I updated the description from
UnplannedtoNot planned yetas that better reflects what this should express.albertoperdomo commentedon Sep 4, 2020
@Hebilicious We definitely have plans to support these additional native types in migrate. It's just not our current top priority (that's releasing a production-ready version of migrate), but we definitely want to add this capability sooner rather than later as we consider this really important (could potentially be in scope for the GA release).
5 remaining items
jeliasson commentedon Nov 14, 2020
Extended native database type support is a preview feature in
2.11.0https://www.prisma.io/docs/concepts/components/preview-features/native-types
https://www.prisma.io/docs/concepts/components/preview-features/native-types/native-types-mappings
tsiege commentedon Jan 21, 2021
Would this feature include Postgres's native
ltreetype? I'm not seeing it here https://www.prisma.io/docs/concepts/components/preview-features/native-types/native-types-mappings#postgresql-connector and I don't see it listed in this table either https://www.prisma.io/docs/concepts/database-connectors/postgresql#introspectionchrissisura commentedon Jan 22, 2021
{ mode: "insensitive" } query option not working with PostgresQL UUID native type because "function lower(uuid) does not exist", which is really annoying because it means prisma studio relation browsing (opening in new Tab) doesn't work as they always query with mode: "insensitive" on
pantharshit00 commentedon Jan 27, 2021
@chrissisura Thanks for reporting. I have opened: #5311
We will address that soon.
albertoperdomo commentedon Feb 1, 2021
In addition to the native types originally in scope and documented here, we are also adding the following types on PostgreSQL:
field String @db.Inetfield Decimal @db.Moneyfield Int @db.Oidfield String @db.Citext(caveat: requires the Citext extension to be enabled)albertoperdomo commentedon Feb 18, 2021
Native types was released in General Availability in 2.17.0. Release notes: https://github.com/prisma/prisma/releases/tag/2.17.0