-
Notifications
You must be signed in to change notification settings - Fork 850
Docs for Prisma ORM v6.16.0 #7096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 20 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
d6b1bb3
update docs for sql views
nikolasburk 6547b29
Update content/200-orm/100-prisma-schema/20-data-model/40-views.mdx
nikolasburk 07b270a
add improvements
nikolasburk cead47b
Merge branch 'docs/6.14.0' of github.com:prisma/docs into docs/6.14.0
nikolasburk d971a75
minor improvements
nikolasburk 95620a8
minor improvements in view docs
nikolasburk bf51ce6
polished view docs
nikolasburk 1d2afdc
update notes about query compiler in ga
nikolasburk 89631fa
fix merge conflicts
nikolasburk 604eb59
update preview feature reference
nikolasburk 2514cca
update management api guide
nikolasburk 1bdecb6
reworked partner oauth section
nikolasburk 12ea903
add docs for creating oauth apps
nikolasburk 3fad666
deprecate postgres extensions
nikolasburk 3208b24
resolve merge conflict
nikolasburk 03d4da0
fix remaining merge conflicts
nikolasburk 62a4bbd
Merge branch 'main' into docs/6.16.0
nikolasburk be83cf2
fix remaining merge conflicts
nikolasburk cdf1558
Merge branch 'docs/6.16.0' of github.com:prisma/docs into docs/6.16.0
nikolasburk caed9fb
Update content/200-orm/100-prisma-schema/80-postgresql-extensions.mdx
nikolasburk 1569814
Update content/200-orm/100-prisma-schema/80-postgresql-extensions.mdx
nikolasburk fc1c971
Update content/200-orm/200-prisma-client/000-setup-and-configuration/…
nikolasburk fc2dee5
fix broken links
nikolasburk 533428e
Merge branch 'docs/6.16.0' of github.com:prisma/docs into docs/6.16.0
nikolasburk 706c2ec
fix broken tab item
nikolasburk a438d2e
fix broken mdx
nikolasburk acfe3df
add docs for vs code login and direct tcp connections
nikolasburk 206d2ed
update query compiler into ga
nikolasburk 7b39a6e
update driver adapters into ga
nikolasburk 751b2c7
update prisma-client generator to ga
nikolasburk 6eb0276
add prisma-client example
nikolasburk 7a11f13
Update content/200-orm/200-prisma-client/000-setup-and-configuration/…
nikolasburk a6093ed
Update content/200-orm/200-prisma-client/000-setup-and-configuration/…
nikolasburk 23c534f
Apply suggestions from code review
nikolasburk 41f1200
fix minor issues
nikolasburk 119edd8
Merge branch 'docs/6.16.0' of github.com:prisma/docs into docs/6.16.0
nikolasburk 9f1a436
Merge branch 'main' into docs/6.16.0
nikolasburk 9b98e6c
fix broken mdx
nikolasburk 2cb83f4
Merge branch 'docs/6.16.0' of github.com:prisma/docs into docs/6.16.0
nikolasburk 80b3f79
fix links
nikolasburk 6c0b3cc
Update content/200-orm/200-prisma-client/000-setup-and-configuration/…
nikolasburk edf852b
Merge branch 'main' of github.com:prisma/docs into docs/6.16.0
nikolasburk 0f373b2
Merge branch 'main' into docs/6.16.0
nikolasburk 460bdea
minor changes
nikolasburk fbfaf11
Merge branch 'docs/6.16.0' of github.com:prisma/docs into docs/6.16.0
nikolasburk a263b4b
remove note about no login for local ppg management in vs code
nikolasburk 506d54c
Merge branch 'main' into docs/6.16.0
nikolasburk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 22 additions & 83 deletions
105
content/200-orm/100-prisma-schema/80-postgresql-extensions.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,52 @@ | ||
--- | ||
title: 'PostgreSQL extensions' | ||
metaTitle: 'How to represent PostgreSQL extensions in your Prisma schema' | ||
metaDescription: 'How to represent PostgreSQL extensions in your Prisma scheme, introspect extensions in your database, and apply changes to extensions with Prisma Migrate' | ||
sidebar_class_name: preview-badge | ||
metaTitle: 'How to use PostgreSQL extensions with Prisma ORM' | ||
metaDescription: 'How to install and manage PostgreSQL extensions with Prisma ORM using customized migrations, and how to use them in Prisma Client.' | ||
tocDepth: 3 | ||
--- | ||
|
||
This page introduces PostgreSQL extensions and describes how to represent extensions in your Prisma schema, how to introspect existing extensions in your database, and how to apply changes to your extensions to your database with Prisma Migrate. | ||
This page is about [PostgreSQL extensions](https://www.postgresql.org/docs/current/external-extensions.html) and explains how to use them with Prisma ORM. | ||
|
||
<Admonition type="warning"> | ||
:::warning | ||
|
||
Support for declaring PostgreSQL extensions in your schema is available in preview for the PostgreSQL connector only in Prisma versions 4.5.0 and later. | ||
Between Prisma ORM v4.5.0 and v6.16.0, you could enable extensions in the Prisma schema via the `postgresqlExtensions` preview feature flag. This feature flag has been deprecated in v6.16.0 and the recommended approach for using PostgreSQL extensions now is to install them via [customized migrations](/orm/prisma-migrate/workflows/customizing-migrations). | ||
|
||
</Admonition> | ||
::: | ||
|
||
## What are PostgreSQL extensions? | ||
|
||
PostgreSQL allows you to extend your database functionality by installing and activating packages known as _extensions_. For example, the `citext` extension adds a case-insensitive string data type. Some extensions, such as `citext`, are supplied directly by PostgreSQL, while other extensions are developed externally. For more information on extensions, see [the PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-createextension.html). | ||
|
||
To use an extension, it must first be _installed_ on the local file system of your database server. You then need to _activate_ the extension, which runs a script file that adds the new functionality. | ||
|
||
<Admonition type="info"> | ||
## Using a PostgreSQL extension with Prisma ORM | ||
|
||
Note that PostgreSQL's documentation uses the term 'install' to refer to what we call activating an extension. We have used separate terms here to make it clear that these are two different steps. | ||
Let's walk through an example of installing the `citext` extension. | ||
|
||
</Admonition> | ||
### 1. Create an empty migration | ||
|
||
Prisma's `postgresqlExtensions` preview feature allows you to represent PostgreSQL extensions in your Prisma schema. Note that specific extensions may add functionality that is not currently supported by Prisma. For example, an extension may add a type or index that is not supported by Prisma. This functionality must be implemented on a case-by-case basis and is not provided by this preview feature. | ||
Run the following command to create an empty migration that you can [customize](/orm/prisma-migrate/workflows/customizing-migrations): | ||
|
||
## How to enable the `postgresqlExtensions` preview feature | ||
|
||
Representing PostgreSQL extensions in your Prisma Schema is currently a preview feature. To enable the `postgresqlExtensions` preview feature, you will need to add the `postgresqlExtensions` feature flag to the `previewFeatures` field of the `generator` block in your Prisma schema: | ||
|
||
```prisma file=schema.prisma highlight=3;add showLineNumbers | ||
generator client { | ||
provider = "prisma-client-js" | ||
//add-next-line | ||
previewFeatures = ["postgresqlExtensions"] | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
```terminal | ||
npx prisma migrate dev --create-only | ||
``` | ||
|
||
## How to represent PostgreSQL extensions in your Prisma schema | ||
### 2. Add a SQL statement to install the extension | ||
|
||
To represent PostgreSQL extensions in your Prisma schema, add the `extensions` field to the `datasource` block of your `schema.prisma` file with an array of the extensions that you require. For example, the following schema lists the `hstore`, `pg_trgm` and `postgis` extensions: | ||
In the new migration file that was created in the `migrations` directory, add the following statement: | ||
|
||
```prisma file=schema.prisma showLineNumbers | ||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
extensions = [hstore(schema: "myHstoreSchema"), pg_trgm, postgis(version: "2.1")] | ||
} | ||
```sql | ||
CREATE EXTENSION IF NOT EXISTS citext; | ||
``` | ||
|
||
Each extension name in the Prisma schema can take the following optional arguments: | ||
|
||
- `schema`: the name of the schema in which to activate the extension's objects. If this argument is not specified, the current default object creation schema is used. | ||
- `version`: the version of the extension to activate. If this argument is not specified, the value given in the extension's control file is used. | ||
- `map`: the database name of the extension. If this argument is not specified, the name of the extension in the Prisma schema must match the database name. | ||
### 3. Deploy the migration | ||
|
||
In the example above, the `hstore` extension uses the `myHstoreSchema` schema, and the `postgis` extension is activated with version 2.1 of the extension. | ||
Run the following command to deploy the migration and apply to your database: | ||
|
||
The `map` argument is useful when the PostgreSQL extension that you want to activate has a name that is not a valid identifier in the Prisma schema. For example, the `uuid-ossp` PostgreSQL extension name is an invalid identifier because it contains a hyphen. In the following example, the extension is mapped to the valid name `uuidOssp` in the Prisma schema: | ||
|
||
```prisma file=schema.prisma showLineNumbers | ||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
extensions = [uuidOssp(map: "uuid-ossp")] | ||
} | ||
```terminal | ||
npx prisma migrate deploy | ||
``` | ||
|
||
## How to introspect PostgreSQL extensions | ||
nikolasburk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
To [introspect](/orm/prisma-schema/introspection) PostgreSQL extensions currently activated in your database and add relevant extensions to your Prisma schema, run `npx prisma db pull`. | ||
|
||
Many PostgreSQL extensions are not relevant to the Prisma schema. For example, some extensions are intended for database administration tasks that do not change the schema. If all these extensions were included, the list of extensions would be very long. To avoid this, Prisma maintains an allowlist of known relevant extensions. The current allowlist is the following: | ||
|
||
- [`citext`](https://www.postgresql.org/docs/current/citext.html): provides a case-insensitive character string type, `citext` | ||
- [`pgcrypto`](https://www.postgresql.org/docs/current/pgcrypto.html): provides cryptographic functions, like `gen_random_uuid()`, to generate universally unique identifiers (UUIDs v4) | ||
- [`uuid-ossp`](https://www.postgresql.org/docs/current/uuid-ossp.html): provides functions, like `uuid_generate_v4()`, to generate universally unique identifiers (UUIDs v4) | ||
- [`postgis`](https://postgis.net/): adds GIS (Geographic Information Systems) support | ||
|
||
**Note**: Since PostgreSQL v13, `gen_random_uuid()` can be used without an extension to generate universally unique identifiers (UUIDs v4). | ||
|
||
Extensions are introspected as follows: | ||
|
||
- The first time you introspect, all database extensions that are on the allowlist are added to your Prisma schema | ||
- When you re-introspect, the behavior depends on whether the extension is on the allowlist or not. | ||
- Extensions on the allowlist: | ||
- are **added** to your Prisma schema if they are in the database but not in the Prisma schema | ||
- are **kept** in your Prisma schema if they are in the Prisma schema and in the database | ||
- are **removed** from your Prisma schema if they are in the Prisma schema but not the database | ||
- Extensions not on the allowlist: | ||
- are **kept** in your Prisma schema if they are in the Prisma schema and in the database | ||
- are **removed** from your Prisma schema if they are in the Prisma schema but not the database | ||
|
||
The `version` argument will not be added to the Prisma schema when you introspect. | ||
|
||
## How to migrate PostgreSQL extensions | ||
|
||
You can update your list of PostgreSQL extensions in your Prisma schema and apply the changes to your database with [Prisma Migrate](/orm/prisma-migrate). | ||
|
||
This works in a similar way to migration of other elements of your Prisma schema, such as models or fields. However, there are the following differences: | ||
### 4. Use the extension | ||
|
||
- If you remove an extension from your schema but it is still activated on your database, Prisma Migrate will not deactivate it from the database. | ||
- If you add a new extension to your schema, it will only be activated if it does not already exist in the database, because the extension may already have been created manually. | ||
- If you remove the `version` or `schema` arguments from the extension definition, it has no effect to the extensions in the database in the following migrations. | ||
You can now use the extension in your queries with Prisma Client. If the extension has special data types that currently can't be natively represented in the Prisma schema, you can still define fields of that type on your models using the [`Unsupported`](/orm/prisma-schema/data-model/models#unsupported-types) fallback type. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.