Skip to main content

原生数据库类型

Prisma Migrate 将 Prisma 架构 中定义的模型转换为数据库中的功能。

¥Prisma Migrate translates the model defined in your Prisma schema into features in your database.

A diagram that shows a Prisma schema on the left (labeled: Prisma schema, models) and a database on the right (labeled: Database, tables). Two parallel arrows connect the schema and the database, showing how '@unique' maps to 'UNIQUE' and '@id' maps to 'PRIMARY KEY'.

数据模型 中的每个 1 功能都映射到底层数据库中的相应功能。如果你可以在 Prisma 架构中定义功能,则 Prisma Migrate 支持该功能。

¥Every¹ feature in your data model maps to a corresponding feature in the underlying database. If you can define a feature in the Prisma schema, it is supported by Prisma Migrate.

有关 Prisma 架构功能的完整列表,请参阅:

¥For a complete list of Prisma schema features, refer to:

Prisma Migrate 还支持将每个字段映射到 特定的原生类型,并且有多种方法映射到 包含数据库中没有 Prisma 架构等效项的功能

¥Prisma Migrate also supports mapping each field to a specific native type, and there are ways to include features without a Prisma schema equivalent in your database.

注意

注释和 Prisma ORM 级函数(uuid()cuid())不映射到数据库功能。

¥Comments and Prisma ORM-level functions (uuid() and cuid()) do not map to database features.

将字段映射到特定的原生类型

¥Mapping fields to a specific native type

每个 Prisma ORM 类型映射到默认的底层数据库类型 - 例如,PostgreSQL 连接器默认将 String 映射到 text原生数据库类型属性 确定应在数据库中创建哪种特定原生类型。

¥Each Prisma ORM type maps to a default underlying database type - for example, the PostgreSQL connector maps String to text by default. Native database type attributes determines which specific native type should be created in the database.

信息

某些 Prisma ORM 类型仅映射到单个原生类型。

¥Some Prisma ORM types only map to a single native type.

在以下示例中,nametitle 字段具有 @db.VarChar(X) 类型属性:

¥In the following example, the name and title fields have a @db.VarChar(X) type attribute:

datasource db {
provider = "postgresql"
}

model User {
id Int @id @default(autoincrement())
name String @db.VarChar(200)
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
title String @db.VarChar(150)
published Boolean @default(true)
authorId Int
author User @relation(fields: [authorId], references: [id])
}

Prisma Migrate 在创建迁移时使用指定的类型:

¥Prisma Migrate uses the specified types when it creates a migration:

  -- CreateTable
CREATE TABLE "User" (
"id" SERIAL,
"name" VARCHAR(200) NOT NULL,
PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Post" (
"id" SERIAL,
"title" VARCHAR(150) NOT NULL,
"published" BOOLEAN NOT NULL DEFAULT true,
"authorId" INTEGER NOT NULL,
PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "Post" ADD FOREIGN KEY("authorId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

按 Prisma ORM 类型进行映射

¥Mappings by Prisma ORM type

对于按 Prisma ORM 类型组织的类型映射,请参阅 Prisma 架构参考 文档。

¥For type mappings organized by Prisma ORM type, refer to the Prisma schema reference documentation.

数据库提供商的映射

¥Mappings by database provider

对于由数据库提供者组织的类型映射,请参阅:

¥For type mappings organized by database provider, see:

处理不支持的数据库功能

¥Handling unsupported database features

Prisma Migrate 无法自动创建 Prisma 架构语言 (PSL) 中没有等效项的数据库功能。例如,目前无法在 PSL 中定义存储过程或部分索引。但是,有多种方法可以使用 Prisma Migrate 将不受支持的功能添加到数据库中:

¥Prisma Migrate cannot automatically create database features that have no equivalent in Prisma Schema Language (PSL). For example, there is currently no way to define a stored procedure or a partial index in PSL. However, there are ways to add unsupported features to your database with Prisma Migrate: