不支持的数据库功能(Prisma Schema)
并非 Prisma ORM 支持的数据库的所有数据库功能和特性都有 Prisma 架构语言等效项。有关受支持功能的完整列表,请参阅 数据库特性矩阵。
¥Not all database functions and features of Prisma ORM's supported databases have a Prisma Schema Language equivalent. Refer to the database features matrix for a complete list of supported features.
原生数据库功能
¥Native database functions
Prisma 模式语言支持多个 functions,你可以使用它们来设置字段的默认值。以下示例使用 Prisma ORM 级别的 uuid()
函数设置 id
字段的值:
¥Prisma Schema Language supports several functions that you can use to set the default value of a field. The following example uses the Prisma ORM-level uuid()
function to set the value of the id
field:
model Post {
id String @id @default(uuid())
}
但是,你也可以使用原生数据库函数在关系数据库上使用 dbgenerated(...)
定义默认值(MongoDB 没有数据库级函数的概念)。以下示例使用 PostgreSQL gen_random_uuid()
函数填充 id
字段:
¥However, you can also use native database functions to define default values with dbgenerated(...)
on relational databases (MongoDB does not have the concept of database-level functions). The following example uses the PostgreSQL gen_random_uuid()
function to populate the id
field:
model User {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
}
何时使用数据库级函数
¥When to use a database-level function
使用数据库级函数有两个原因:
¥There are two reasons to use a database-level function:
-
没有等效的 Prisma ORM 函数(例如 PostgreSQL 中的
gen_random_bytes
)。¥There is no equivalent Prisma ORM function (for example,
gen_random_bytes
in PostgreSQL). -
你不能或不想依赖诸如
uuid()
和cuid()
之类的函数,这些函数仅在 Prisma ORM 级别实现,并且不会在数据库中体现。¥You cannot or do not want to rely on functions such
uuid()
andcuid()
, which are only implemented at a Prisma ORM level and do not manifest in the database.考虑以下示例,它将
id
字段设置为随机生成的UUID
:¥Consider the following example, which sets the
id
field to a randomly generatedUUID
:model Post {
id String @id @default(uuid())
}仅当你使用 Prisma 客户端创建
Post
时才会生成 UUID。如果你以任何其他方式创建帖子,例如用纯 SQL 编写的批量导入脚本,则必须自行生成 UUID。¥The UUID is only generated if you use Prisma Client to create the
Post
. If you create posts in any other way, such as a bulk import script written in plain SQL, you must generate the UUID yourself.
为原生数据库功能启用 PostgreSQL 扩展
¥Enable PostgreSQL extensions for native database functions
在 PostgreSQL 中,一些原生数据库函数是扩展的一部分。例如,在 PostgreSQL 版本 12.13 及更早版本中,gen_random_uuid()
函数是 pgcrypto
扩展的一部分。
¥In PostgreSQL, some native database functions are part of an extension. For example, in PostgreSQL versions 12.13 and earlier, the gen_random_uuid()
function is part of the pgcrypto
extension.
要使用 PostgreSQL 扩展,你必须首先将其安装在数据库服务器的文件系统上。
¥To use a PostgreSQL extension, you must first install it on the file system of your database server.
在 Prisma ORM 版本 4.5.0 及更高版本中,你可以通过在 Prisma 架构中使用 postgresqlExtensions
预览功能 声明扩展来激活扩展:
¥In Prisma ORM versions 4.5.0 and later, you can then activate the extension by declaring it in your Prisma schema with the postgresqlExtensions
preview feature:
generator client {
provider = "prisma-client-js"
previewFeatures = ["postgresqlExtensions"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
extensions = [pgcrypto]
}
在 Prisma ORM 的早期版本中,你必须运行 SQL 命令来激活扩展:
¥In earlier versions of Prisma ORM, you must instead run a SQL command to activate the extension:
CREATE EXTENSION IF NOT EXISTS pgcrypto;
如果你的项目使用 Prisma 迁移 ,则必须 作为迁移的一部分安装扩展 。 不要手动安装该扩展,因为影子数据库也需要它。
¥If your project uses Prisma Migrate, you must install the extension as part of a migration . Do not install the extension manually, because it is also required by the shadow database.
如果扩展不可用,Prisma Migrate 将返回以下错误:
¥Prisma Migrate returns the following error if the extension is not available:
Migration `20210221102106_failed_migration` failed to apply cleanly to a temporary database.
Database error: Error querying the database: db error: ERROR: type "pgcrypto" does not exist
不支持的字段类型
¥Unsupported field types
关系数据库的某些数据库类型(例如 polygon
或 geometry
)没有等效的 Prisma 架构语言。使用 Unsupported
字段类型来表示 Prisma 架构中的字段:
¥Some database types of relational databases, such as polygon
or geometry
, do not have a Prisma Schema Language equivalent. Use the Unsupported
field type to represent the field in your Prisma schema:
model Star {
id Int @id @default(autoincrement())
position Unsupported("circle")? @default(dbgenerated("'<(10,4),11>'::circle"))
}
prisma migrate dev
和 prisma db push
命令都将在数据库中创建 circle
类型的 position
字段。但是,该字段在生成的 Prisma 客户端中不可用。
¥The prisma migrate dev
and prisma db push
command will both create a position
field of type circle
in the database. However, the field will not be available in the generated Prisma Client.
不支持的数据库功能
¥Unsupported database features
某些功能(例如 SQL 视图或部分索引)无法在 Prisma 架构中表示。如果你的项目使用 Prisma 迁移 ,则必须 将不支持的功能包含在迁移过程中 。
¥Some features, like SQL views or partial indexes, cannot be represented in the Prisma schema. If your project uses Prisma Migrate, you must include unsupported features as part of a migration .