Postgres 扩展
概述
¥Overview
Prisma Postgres 支持以下 PostgreSQL 扩展:
¥Prisma Postgres supports the following PostgreSQL extensions:
即将支持 更多扩展。如果你希望在 Prisma Postgres 中看到特定的扩展,填写此表单。
¥Support for more extensions will follow soon. If there are specific extensions you'd like to see in Prisma Postgres, fill out this form.
Prisma 中的 Postgres 扩展支持:Postgres 目前在 抢先体验 版本中,尚不推荐用于生产场景。
¥Postgres extensions support in Prisma Postgres is currently in Early Access and not yet recommended for production scenarios.
将扩展与 Prisma ORM 结合使用
¥Using extensions with Prisma ORM
某些扩展可能已受 Prisma Postgres 支持,但尚未受 Prisma ORM 支持。Prisma ORM 即将原生支持部分 Postgres 扩展。同时,你仍然可以通过使用 自定义迁移 和 TypedSQL(或在 Prisma ORM 中通过其他机制发送 原始 SQL)将这些扩展与 Prisma ORM 一起使用。
¥Some extensions may already be supported by Prisma Postgres but not yet by Prisma ORM. Native support for some Postgres extensions in Prisma ORM is coming soon. In the meantime, you can still use these extensions with Prisma ORM by using customized migrations and TypedSQL (or another mechanism to send raw SQL via in Prisma ORM).
让我们以 pgvector
为例进行演示。
¥Let's walk through an example with pgvector
.
1. 创建空迁移文件
¥ Create an empty migration file
要自定义迁移,请先创建一个空的迁移文件:
¥To customize a migration, first create an empty migration file:
npx prisma migrate dev --name add-pgvector --create-only
请注意 --create-only
标志,它将在你的迁移目录中创建一个空的迁移文件。
¥Notice the --create-only
flag which will create an empty migration file in your migrations directory.
2. 在迁移文件中创建并使用扩展
¥ Create and use the extension in your migration file
在空的迁移文件中,你可以编写任何要在数据库中执行的自定义 SQL:
¥In the empty migration file, you can write any custom SQL to be executed in the database:
-- prisma/migrations/<timestamp>-add-pgvector/migration.sql
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE "Document" (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
embedding VECTOR(4) -- use 4 for demo purposes; real-world values are much bigger
);
在这种情况下,你需要:
¥In this case, you:
-
使用
CREATE EXTENSION
语句在数据库中安装pgvector
扩展¥install the
pgvector
extension in your database using theCREATE EXTENSION
statement -
创建一个使用该扩展中的
VECTOR
类型的Document
表¥create a
Document
table that uses theVECTOR
type from that extension
3. 针对数据库执行迁移
¥ Execute the migration against the database
运行以下命令执行迁移并将其更改应用于你的数据库:
¥Run the following command to execute the migration and apply its changes in your database:
npx prisma migrate deploy
此命令将应用待处理的 prisma/migrations/<timestamp>-add-pgvector/migration.sql
迁移,并在数据库中创建 Document
表。
¥This command will apply the pending prisma/migrations/<timestamp>-add-pgvector/migration.sql
migration and create the Document
table in your database.
4. 将文档表拉入 Prisma 模式
¥ Pull the document table into your Prisma schema
使用新的 Document
表检查数据库模式,并用它来更新 Prisma 模式:
¥Introspect the database schema with the new Document
table and update your Prisma schema with it:
npx prisma db pull
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "accelerate.prisma-data.net"
✔ Introspected 3 models and wrote them into prisma/schema.prisma in 3.23s
*** WARNING ***
These fields are not supported by Prisma Client, because Prisma currently does not support their types:
- Model: "Document", field: "embedding", original data type: "vector"
Run prisma generate to generate Prisma Client.
之所以使用 预计命令的 CLI 输出中会出现警告,是因为 Prisma ORM 尚未原生支持 VECTOR
类型。
¥The warning in the CLI output of the command is expected because Prisma ORM doesn't natively support the VECTOR
type yet.
你的 Prisma 模式现在将包含 Document
模型:
¥You Prisma schema will now contain the Document
model:
model Document {
id Int @id @default(autoincrement())
title String
embedding Unsupported("vector")?
}
由于 Prisma ORM 尚未原生支持 VECTOR
类型,因此它被表示为 Unsupported
类型。
¥Because the VECTOR
type is not yet natively supported by Prisma ORM, it's represented as an Unsupported
type.
4. 使用原始 SQL 查询
¥ Query with raw SQL
以下是向 Document
表中插入新行的示例查询:
¥Here's an example query for inserting a new row into the Document
table:
await prisma.$executeRaw`
INSERT INTO "Document" (title, embedding)
VALUES ('My Title', '[1,22,1,42]'::vector)
`;
你还可以使用 TypedSQL 对数据库进行类型安全的 SQL 查询。
¥You can also use TypedSQL for type-safe SQL queries against your database.
临时限制
¥Temporary limitations
pgvector
仅适用于新创建的 Prisma Postgres 实例
¥pgvector
is only available on newly created Prisma Postgres instances
目前,pgvector
仅适用于新创建的 Prisma Postgres 实例。它将很快在现有实例中推出。
¥For now, pgvector
is only available on newly created Prisma Postgres instances. It will be rolled out for existing instances soon.
Prisma Studio 不支持 pgvector
¥No Prisma Studio support for pgvector
Prisma Studio 目前不支持使用 pgvector
扩展类型的表。它将显示以下错误:
¥Prisma Studio currently doesn't support tables where types of the pgvector
extensions are used. It will show the following error:
Show Prisma Studio error message
{
"error": "KnownError { message: \"Raw query failed. Code: `N/A`. Message: `Failed to deserialize column of type 'vector'. If you're using $queryRaw and this column is explicitly marked as `Unsupported` in your Prisma schema, try casting this column to any supported Prisma type such as `String`.`\", meta: Object {\"code\": String(\"N/A\"), \"message\": String(\"Failed to deserialize column of type 'vector'. If you're using $queryRaw and this column is explicitly marked as `Unsupported` in your Prisma schema, try casting this column to any supported Prisma type such as `String`.\")}, error_code: \"P2010\" }",
"user_facing_error": {
"is_panic": false,
"message": "Raw query failed. Code: `N/A`. Message: `Failed to deserialize column of type 'vector'. If you're using $queryRaw and this column is explicitly marked as `Unsupported` in your Prisma schema, try casting this column to any supported Prisma type such as `String`.`",
"meta": {
"code": "N/A",
"message": "Failed to deserialize column of type 'vector'. If you're using $queryRaw and this column is explicitly marked as `Unsupported` in your Prisma schema, try casting this column to any supported Prisma type such as `String`."
},
"error_code": "P2010"
}
}
其他扩展即将推出
¥Other extensions are coming soon
即将支持以下扩展:
¥Support for the following extensions is going to come soon:
如果你希望看到特定的扩展,填写此表单。
¥If there are specific extensions you'd like to see, fill out this form.