Skip to main content

原生数据库功能

在 PostgreSQL 中,一些 原生数据库函数 是可选扩展的一部分。例如,在 PostgreSQL 版本 12.13 及更早版本中,gen_random_uuid() 函数是 pgcrypto 扩展的一部分。

¥In PostgreSQL, some native database functions are part of optional extensions. For example, in PostgreSQL versions 12.13 and earlier the gen_random_uuid() function is part of the pgcrypto extension.

要使用 PostgreSQL 扩展,你必须将其安装在数据库服务器的文件系统上,然后激活该扩展。如果你使用 Prisma Migrate,则必须将其作为迁移的一部分来完成。

¥To use a PostgreSQL extension, you must install it on the file system of your database server and then activate the extension. If you use Prisma Migrate, this must be done as part of a migration.

warning

如果你使用 Prisma Migrate,请勿激活迁移文件外部的扩展。影子数据库 需要相同的扩展。Prisma Migrate 会自动创建和删除影子数据库,因此激活扩展的唯一方法是将其包含在迁移文件中。

¥Do not activate extensions outside a migration file if you use Prisma Migrate. The shadow database requires the same extensions. Prisma Migrate creates and deletes the shadow database automatically, so the only way to activate an extension is to include it in a migration file.

在 Prisma ORM 版本 4.5.0 及更高版本中,你可以通过在 Prisma 架构中使用 postgresqlExtensions 预览功能 声明扩展来激活扩展:

¥In Prisma ORM versions 4.5.0 and later, you can activate the extension by declaring it in your Prisma schema with the postgresqlExtensions preview feature:

schema.prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["postgresqlExtensions"]
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
extensions = [pgcrypto]
}

然后,你可以使用 Prisma Migrate 将这些更改应用到你的数据库。详情请参见 如何迁移 PostgreSQL 扩展

¥You can then apply these changes to your database with Prisma Migrate. See How to migrate PostgreSQL extensions for details.

在 Prisma ORM 的早期版本中,你必须将 SQL 命令添加到迁移文件中才能激活扩展。参见 如何在迁移过程中安装 PostgreSQL 扩展

¥In earlier versions of Prisma ORM, you must instead add a SQL command to your migration file to activate the extension. See How to install a PostgreSQL extension as part of a migration.

如何在迁移过程中安装 PostgreSQL 扩展

¥How to install a PostgreSQL extension as part of a migration

本节介绍如何将 SQL 命令添加到迁移文件中以激活 PostgreSQL 扩展。如果你使用 postgresqlExtensions 预览功能管理 Prisma Schema 中的 PostgreSQL 扩展,请参阅 如何迁移 PostgreSQL 扩展

¥This section describes how to add a SQL command to a migration file to activate a PostgreSQL extension. If you manage PostgreSQL extensions in your Prisma Schema with the postgresqlExtensions preview feature instead, see How to migrate PostgreSQL extensions.

以下示例演示了如何在迁移过程中安装 pgcrypto 扩展:

¥The following example demonstrates how to install the pgcrypto extension as part of a migration:

  1. 将具有原生数据库函数的字段添加到你的架构中:

    ¥Add the field with the native database function to your schema:

    model User {
    id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
    }

    如果包含强制转换运算符(例如 ::TEXT),则必须用括号将整个函数括起来:

    ¥If you include a cast operator (such as ::TEXT), you must surround the entire function with parentheses:

    @default(dbgenerated("(gen_random_uuid()::TEXT)"))
  2. 使用 --create-only 标志生成新的迁移而不应用它:

    ¥Use the --create-only flag to generate a new migration without applying it:

    npx prisma migrate dev --create-only
  3. 打开生成的 migration.sql 文件并启用 pgcrypto 模块:

    ¥Open the generated migration.sql file and enable the pgcrypto module:

    CREATE EXTENSION IF NOT EXISTS pgcrypto;

    ADD COLUMN "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    ADD PRIMARY KEY ("id");
  4. 应用迁移:

    ¥Apply the migration:

    npx prisma migrate dev

每次重置数据库或向团队添加新成员时,所有必需的功能都是迁移历史记录的一部分。

¥Each time you reset the database or add a new member to your team, all required functions are part of the migration history.