Skip to content

Epic: Support Native Database Types #3447

Closed

Description

@matthewmueller

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, and money.
  • MySQL offers data types like blob, varbinary, and set.

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 Int could map to a integer, smallint or bigint.
  • 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 Int to integer, but we cannot map Int to bigint. 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 money type 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 String is compatible with @mydb.VarChar(x) and @mydb.Text. But String is 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

  1. Done/In Preview Add support for native types in the Schema.
  2. Done/In Preview Update Introspect to understand and generate Prisma Schemas with native types.
  3. Done/In Preview Add support for native types in Prisma Migrate
  4. Done/In Preview Unsupported type: 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
  5. Done/In Preview dbgenerated(STRING): Make it possible to encode raw database defaults in the @default attribute of fields
  6. Done/In Preview @@ignore attribute for models and @ignore attribute 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
  7. Done/In Preview Upgrading Client to take advantage of native types is still up for discussion.
  8. Done/In Preview Studio has basic support for native types

Related issues

Additional context

Activity

  1. changed the title [-]Support Native Types[/-] [+]Support Database Native Types[/+] on Aug 27, 2020
  2. changed the title [-]Support Database Native Types[/-] [+]Epic: Support Database Native Types[/+] on Aug 27, 2020
  3. changed the title [-]Epic: Support Database Native Types[/-] [+]Epic: Support Native Database Types[/+] on Aug 28, 2020
  4. Hebilicious commented on Sep 3, 2020

    @Hebilicious

    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.

  5. janpio commented on Sep 3, 2020

    @janpio
    Contributor

    I updated the description from Unplanned to Not planned yet as that better reflects what this should express.

  6. albertoperdomo commented on Sep 4, 2020

    @albertoperdomo
    Contributor

    @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).

  7. added this to the Backlog 2.9.0 milestone on Sep 30, 2020
  8. 5 remaining items

  9. added this to the Backlog 2.11.0 milestone on Oct 28, 2020
  10. jeliasson commented on Nov 14, 2020

    @jeliasson
  11. tsiege commented on Jan 21, 2021

    @tsiege
  12. chrissisura commented on Jan 22, 2021

    @chrissisura

    { 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

  13. pantharshit00 commented on Jan 27, 2021

    @pantharshit00
    Contributor

    @chrissisura Thanks for reporting. I have opened: #5311

    We will address that soon.

  14. albertoperdomo commented on Feb 1, 2021

    @albertoperdomo
    Contributor

    In addition to the native types originally in scope and documented here, we are also adding the following types on PostgreSQL:

    • Inet, mapped to String, e.g. field String @db.Inet
    • Money, mapped to Decimal, e.g. field Decimal @db.Money
    • Oid, mapped to Int, e.g. field Int @db.Oid
    • Citext, mapped to String, e.g. field String @db.Citext (caveat: requires the Citext extension to be enabled)
  15. albertoperdomo commented on Feb 18, 2021

    @albertoperdomo
    Contributor
  16. removed their assignment
    on May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions