跳到主要內容

原生資料庫類型

Prisma Migrate 將您在 Prisma schema 中定義的模型轉換為您資料庫中的功能。

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'.

資料模型 中的每個¹功能都對應到基礎資料庫中的相應功能。如果您可以在 Prisma schema 中定義一個功能,Prisma Migrate 就支援它。

如需 Prisma schema 功能的完整列表,請參閱

  • 資料庫功能矩陣,其中列出了資料庫功能以及它們在 Prisma schema 中對應的內容。
  • Prisma schema 參考,其中列出了所有 Prisma schema 功能,包括欄位類型、屬性和函數。

Prisma Migrate 也支援將每個欄位映射到特定的原生類型,並且有一些方法可以在您的資料庫中包含沒有 Prisma schema 等效項的功能

注意

註解和 Prisma ORM 層級函數 (uuid()cuid()) 不會映射到資料庫功能。

將欄位映射到特定的原生類型

每個 Prisma ORM 類型都映射到預設的底層資料庫類型 - 例如,PostgreSQL 連接器預設將 String 映射到 text原生資料庫類型屬性決定了應該在資料庫中建立哪個特定原生類型。

資訊

注意:某些 Prisma ORM 類型僅映射到單一原生類型。

在以下範例中,nametitle 欄位具有 @db.VarChar(X) 類型屬性

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

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 在建立遷移時使用指定的類型

  -- 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 類型映射

如需依 Prisma ORM 類型組織的類型映射,請參閱Prisma schema 參考文件。

依資料庫供應商映射

如需依資料庫供應商組織的類型映射,請參閱

處理不支援的資料庫功能

Prisma Migrate 無法自動建立在 Prisma Schema Language (PSL) 中沒有等效項的資料庫功能。例如,目前無法在 PSL 中定義預存程序或部分索引。但是,有一些方法可以使用 Prisma Migrate 將不支援的功能新增到您的資料庫中