跳過至主要內容

原生資料庫型別

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 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 將不支援的功能新增到您的資料庫中

© . This site is unofficial and not affiliated with Prisma Data, Inc.