Prisma CLI 參考文件
本文件說明了 Prisma CLI 的指令、參數與選項。
指令
version (-v)
version 指令會輸出目前 prisma 版本、平台以及引擎二進位檔的資訊。
選項
version 指令支援以下選項來修改其行為
| 選項 | 必填 | 描述 |
|---|---|---|
--json | 否 | 以 JSON 格式輸出版本資訊。 |
範例
輸出版本資訊
prisma version
Environment variables loaded from .env
prisma : 2.21.0-dev.4
@prisma/client : 2.21.0-dev.4
Current platform : windows
Query Engine : query-engine 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\query-engine-windows.exe)
Migration Engine : migration-engine-cli 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\migration-engine-windows.exe)
Format Binary : prisma-fmt 60ba6551f29b17d7d6ce479e5733c70d9c00860e (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : 60ba6551f29b17d7d6ce479e5733c70d9c00860e
Studio : 0.365.0
輸出版本資訊 (-v)
prisma -v
Environment variables loaded from .env
prisma : 2.21.0-dev.4
@prisma/client : 2.21.0-dev.4
Current platform : windows
Query Engine : query-engine 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\query-engine-windows.exe)
Migration Engine : migration-engine-cli 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\migration-engine-windows.exe)
Format Binary : prisma-fmt 60ba6551f29b17d7d6ce479e5733c70d9c00860e (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : 60ba6551f29b17d7d6ce479e5733c70d9c00860e
Studio : 0.365.0
以 JSON 格式輸出版本資訊
prisma version --json
Environment variables loaded from .env
{
"prisma": "2.21.0-dev.4",
"@prisma/client": "2.21.0-dev.4",
"current-platform": "windows",
"query-engine": "query-engine 60ba6551f29b17d7d6ce479e5733c70d9c00860e (at node_modules\\@prisma\\engines\\query-engine-windows.exe)",
"migration-engine": "migration-engine-cli 60ba6551f29b17d7d6ce479e5733c70d9c00860e (at node_modules\\@prisma\\engines\\migration-engine-windows.exe)",
"format-binary": "prisma-fmt 60ba6551f29b17d7d6ce479e5733c70d9c00860e (at node_modules\\@prisma\\engines\\prisma-fmt-windows.exe)",
"default-engines-hash": "60ba6551f29b17d7d6ce479e5733c70d9c00860e",
"studio": "0.365.0"
}
init
在當前目錄中引導建立一個全新的 Prisma ORM 專案。
init 指令不會解析任何現有檔案。相反地,它會在您目前的目錄下建立一個 prisma 目錄,其中包含一個基本的 schema.prisma 檔案。
預設情況下,此專案會設定一個 本地 Prisma Postgres 執行個體,但您可以使用 --datasource-provider 選項選擇不同的資料庫。
引數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--datasource-provider | 否 | 指定 datasource 區塊中 provider 欄位的值。選項包括 prisma+postgres、sqlite、postgresql、mysql、sqlserver、mongodb 和 cockroachdb。 | postgresql |
--db | 否 | --datasource-provider prisma+postgres 的簡寫語法;建立一個新的 Prisma Postgres 執行個體。需要在. | |
--prompt (或 --vibe) | 否 | 根據提示詞 (prompt) 構建 Prisma schema,並將其部署到新的 Prisma Postgres 執行個體。需要在. | |
--url | 否 | 定義自定義的 datasource url。 | |
--generator-provider | 否 | 定義要使用的產生器 (generator) 供應商。 | prisma-client |
--preview-feature | 否 | 定義要使用的 預覽功能 (Preview features)。若要定義多個預覽功能,您必須為每個功能分別提供此旗標。請參閱範例。 | |
--output | 否 | 指定 產生的 client 輸出位置。 | ../generated/prisma |
--with-model | 否 | 在初始 Prisma schema 中新增一個簡單的 User 模型。自版本 5.14.0 起可用。 |
範例
執行 prisma init
prisma init
npx prisma init
Initialized Prisma in your project
prisma/
schema.prisma
prisma.config.ts
Next, choose how you want to set up your database:
CONNECT EXISTING DATABASE:
1. Configure your DATABASE_URL in `prisma.config.ts`
2. Run `npx prisma db pull` to introspect your database.
CREATE NEW DATABASE:
Local: npx prisma dev (runs Postgres locally in your terminal)
Cloud: npx create-db (creates a free Prisma Postgres database)
Then, define your models in `prisma/schema.prisma` and run `npx prisma migrate dev` to apply your schema.
Learn more: https://pris.ly/getting-started
接下來,執行 prisma dev 指令來與您的本地 Prisma Postgres 執行個體互動(例如執行遷移或執行查詢)。
執行 prisma init --datasource-provider sqlite
prisma init --datasource-provider sqlite
指令輸出包含有關如何使用產生的檔案以及如何在專案中開始使用 Prisma ORM 的有用資訊。
執行 prisma init --db
prisma init --db
✓ Select an authentication method Google
Authenticating to Prisma Platform via browser.
Visit the following URL in your browser to authenticate:
https://console.prisma.io/auth/cli?state=eyJjb6ll...
Successfully authenticated as amanyoyoyo@gmail.com.
Let's set up your Prisma Postgres database!
✓ Select your region: ap-southeast-1 - Asia Pacific (Singapore)
✓ Enter a project name: My Prisma Project
✓ Success! Your Prisma Postgres database is ready ✅
We found an existing schema.prisma file in your current project directory.
--- Database URL ---
Connect Prisma ORM to your Prisma Postgres database with this URL:
--- Next steps ---
Go to https://pris.ly/ppg-init for detailed instructions.
1. Install the Postgres adapter
npm install @prisma/adapter-pg
...and add it to your Prisma Client instance:
import { PrismaClient } from "./generated/prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";
const connectionString = `${process.env.DATABASE_URL}`;
const adapter = new PrismaPg({ connectionString });
const prisma = new PrismaClient({ adapter });
2. Apply migrations
Run the following command to create and apply a migration:
npx prisma migrate dev
3. Manage your data
View and edit your data locally by running this command:
npx prisma studio
...or online in Console:
https://console.prisma.io/cmhyn0uwl0q6903foel16ff31/cmhyn143t074tyLfoezs684ag/cmhyn143t074uylfon8hfre5z/studio
4. Send queries from your app
If you already have an existing app with Prisma ORM, you can now run it and it will send queries against your newly created Prisma Postgres instance.
5. Learn more
For more info, visit the Prisma Postgres docs: https://pris.ly/ppg-docs
該指令會建立一個新的 Prisma Postgres 執行個體。請注意,這需要您已通過驗證,如果您是第一次執行且未經過驗證,該指令將為您打開瀏覽器以登入控制台 (Console)。
執行 prisma init --prompt "Simple habit tracker application"
prisma init --prompt "Simple habit tracker application"
該指令會構建 Prisma schema 並將其部署到新的 Prisma Postgres 執行個體。請注意,這需要您已通過驗證驗證,如果您是第一次執行且未經過驗證,該指令將為您打開瀏覽器以登入控制台 (Console)。
執行 prisma init --preview-feature
prisma init --preview-feature metrics
datasource db {
provider = "postgresql"
}
generator client {
provider = "prisma-client"
previewFeatures = ["metrics"]
}
prisma init --preview-feature view --preview-feature metrics
datasource db {
provider = "postgresql"
}
generator client {
provider = "prisma-client"
previewFeatures = ["views", "metrics"]
}
產生的資產
prisma/schema.prisma
一個初始的 schema.prisma 檔案,用於定義您的 schema
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "postgresql"
}
prisma.config.ts
一個用於 Prisma 的 TypeScript 設定檔,定義您的資料來源 URL 和其他設定
import { defineConfig, env } from 'prisma/config'
export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
})
詳情請參閱 Prisma 設定參考。
.env
一個為您的專案定義環境變數的檔案
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
.gitignore
一個指定 git 在您的專案中應忽略哪些資料夾/檔案的檔案。
node_modules
# Keep environment variables out of version control
.env
/generated/prisma
執行 prisma init --url mysql://user:password@localhost:3306/mydb
帶有 --url 參數的 init 指令允許您在 Prisma 初始化期間指定自定義資料來源 URL,而不是依賴佔位符資料庫 URL
prisma init --url mysql://user:password@localhost:3306/mydb
產生的資產
prisma/schema.prisma
一個最小化的 schema.prisma 檔案,用於定義您的 schema
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "mysql"
}
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
prisma.config.ts
一個帶有自定義 URL 的 Prisma TypeScript 設定檔
import { defineConfig, env } from 'prisma/config'
export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
})
.env
一個為您的專案定義環境變數的檔案
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="mysql://user:password@localhost:3306/mydb"
generate
generate 指令根據 prisma/schema.prisma 檔案中定義的 generator 和 data model 區塊產生資產(如 Prisma Client)。
generate 指令最常用於透過 prisma-client 產生器產生 Prisma Client。它會執行以下操作:
- 檢查當前目錄以找到要處理的 Prisma Schema。
- 根據您的 schema 在產生器區塊指定的輸出目錄中產生自定義的 Prisma Client。
先決條件
要使用 generate 指令,您必須在 schema.prisma 檔案中加入產生器定義。用於產生 Prisma Client 的 prisma-client 產生器,可以透過在 schema.prisma 檔案中包含以下內容來新增:
generator client {
provider = "prisma-client"
output = "./generated"
}
選項
| 選項 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--data-proxy | 否 | generate 指令將產生供 Prisma 5.0.0 之前的 Prisma Accelerate 使用的 Prisma Client。與 --accelerate 和 --no-engine 互斥。 | |
--accelerate | 否 | generate 指令將產生供 Prisma Accelerate 使用的 Prisma Client。與 --data-proxy 和 --no-engine 互斥。於 Prisma 5.1.0 及更高版本提供。 | |
--no-engine | 否 | generate 指令將產生不含附帶引擎的 Prisma Client,供 Prisma Accelerate 使用。與 --data-proxy 和 --accelerate 互斥。於 Prisma ORM 5.2.0 及更高版本提供。 | |
--no-hints | 否 | generate 指令在產生 Prisma Client 時,不會在終端機列印使用提示、問卷或資訊橫幅。於 Prisma ORM 5.16.0 及更高版本提供。 | |
--allow-no-models | 否 | generate 指令將在不產生任何模型的情況下產生 Prisma Client。 | |
--watch | 否 | generate 指令將持續監視 schema.prisma 檔案,並在檔案變更時重新產生 Prisma Client。 |
棄用警告
自 Prisma 5.2.0 起,--data-proxy 和 --accelerate 已被棄用,改用 --no-engine,因為 Prisma Client 不再需要特定選項即可與 Prisma Accelerate 配合運作。所有選項仍然可用且運作方式相似,但我們建議使用 --no-engine,因為它能防止下載引擎,這將大幅縮小部署到 serverless 和 edge functions 的應用程式體積。
參數
| 參數 | 必填 | 描述 | 預設值 | |
|---|---|---|---|---|
--schema | 否 | 指定要處理的 schema.prisma 檔案路徑,而非使用預設路徑。支援絕對路徑與相對路徑。 | ./schema.prisma, ./prisma/schema.prisma | |
--generator | 否 | 指定要用於產生資產的產生器。此選項可多次提供以包含多個產生器。預設情況下,將執行目標 schema 中的所有產生器。 |
範例
使用預設 schema.prisma 路徑產生 Prisma Client
prisma generate
✔ Generated Prisma Client to ./node_modules/.prisma/client in 61ms
You can now start using Prisma Client in your code:
import { PrismaClient } from '../prisma/generated/client'
// or const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
Explore the full API: https://pris.ly/d/client
使用非預設 schema.prisma 路徑產生 Prisma Client
prisma generate --schema=./alternative/schema.prisma
持續監視 schema.prisma 檔案變更以自動重新產生 Prisma Client
prisma generate --watch
Watching... /home/prismauser/prisma/prisma-play/prisma/schema.prisma
✔ Generated Prisma Client to ./node_modules/.prisma/client in 45ms
僅使用特定產生器執行 generate 指令
prisma generate --generator client
使用多個特定產生器執行 generate 指令
prisma generate --generator client --generator zod_schemas
產生的資產
prisma-client 產生器會在 output 欄位指定的自定義輸出目錄中建立一個用於操作資料庫的自定義 client - 您可以 自定義輸出資料夾。
validate
驗證 Prisma schema 檔案的 Prisma Schema Language。
參數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--schema | 否 | 指定要處理的 schema.prisma 檔案路徑,而非使用預設路徑。支援絕對路徑與相對路徑。 | ./schema.prisma, ./prisma/schema.prisma |
範例
驗證無錯誤的 schema
prisma validate
驗證有錯誤的 schema
prisma validate
format
格式化 Prisma schema 檔案,包括驗證、格式化以及持久化 schema。
參數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--schema | 否 | 指定要處理的 schema.prisma 檔案路徑,而非使用預設路徑。支援絕對路徑與相對路徑。 | ./schema.prisma, ./prisma/schema.prisma |
--check | 否 | 如果任何檔案未經格式化,則執行失敗。這可用於 CI 偵測 schema 是否已正確格式化 |
範例
驗證無錯誤的 schema
prisma format
對包含驗證錯誤的 schema 進行格式化
prisma format
debug
印出除錯和錯誤報告所需的資訊。
此功能自 5.6.0 及更新版本起提供。
參數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--schema | 否 | 指定要處理的 schema.prisma 檔案路徑,而非使用預設路徑。支援絕對路徑與相對路徑。 | ./schema.prisma, ./prisma/schema.prisma |
--help / --h | 否 | 顯示說明訊息 |
範例
prisma debug
如果您使用的是舊版本的 Prisma,可以透過執行以下指令來使用此功能
npx prisma@latest debug
dev
dev 指令啟動一個 本地 Prisma Postgres 資料庫,您可以對其執行 Prisma ORM 指令。這對於開發和測試非常有用,並且讓您能夠輕鬆地在生產環境中切換到 Prisma Postgres。
參數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--name (或 -n) | 否 | 啟用對特定資料庫執行個體的定位。了解更多。 | |
--port (或 -p) | 否 | 本地 Prisma Postgres HTTP 伺服器監聽的主要埠號。 | 51213 |
--db-port (或 -P) | 否 | 本地 Prisma Postgres 資料庫伺服器監聽的埠號。 | 51214 |
--shadow-db-port | 否 | 影子資料庫 (shadow database) 伺服器監聽的埠號。 | 51215 |
--debug | 否 | 啟用除錯記錄 (debug logging)。 | false |
範例
執行 prisma dev
prisma dev
$ npx prisma dev
Fetching latest updates for this subcommand...
✔ Great Success! 😉👍
Your prisma dev server default is ready and listening on ports 63567-63569.
╭──────────────────────────────╮
│[q]uit [h]ttp url [t]cp urls│
╰──────────────────────────────╯
dev stop
停止一個或多個 本地 Prisma Postgres 資料庫
npx prisma dev stop <glob>
<glob> 是一個 glob 模式的佔位符,用於指定應停止哪些本地 Prisma Postgres 執行個體,例如
npx prisma dev stop mydb # stops a DB called `mydb`
要停止所有以 mydb 開頭的資料庫(例如 mydb-dev 和 mydb-prod),您可以使用 glob
npx prisma dev stop mydb* # stops all DBs starting with `mydb`
dev rm
從您的檔案系統中移除一個或多個 本地 Prisma Postgres 資料庫的資料
npx prisma dev rm <glob>
<glob> 是一個 glob 模式的佔位符,用於指定應移除哪些本地 Prisma Postgres 執行個體,例如
npx prisma dev stop mydb # stops a DB called `mydb`
要停止所有以 mydb 開頭的資料庫(例如 mydb-dev 和 mydb-prod),您可以使用 glob
npx prisma dev stop mydb* # stops all DBs starting with `mydb`
db
db pull
db pull 指令會連接到您的資料庫,並在您的 Prisma schema 中加入反映當前資料庫結構的 Prisma 模型。
警告:此指令將以新 schema 覆寫目前的 schema.prisma 檔案。某些手動更改或自定義設定可能會遺失。如果在執行 db pull 之前檔案包含重要修改,請務必備份目前的 schema.prisma 檔案(或將目前狀態提交到版本控制以便還原)。
在 MongoDB 連接器上使用 db pull 指令進行內省 (Introspection) 時,會對資料進行採樣而非讀取 schema。
先決條件
在使用 db pull 指令之前,您必須在 prisma.config.ts 檔案中設定資料庫連接。
例如
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "sqlite"
}
import { defineConfig, env } from 'prisma/config'
export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
})
選項
| 選項 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--force | 否 | 強制覆寫對 schema 所做的手動更改。產生的 schema 將僅基於內省出的結構。 | |
--print | 否 | 將建立的 schema.prisma 印到螢幕上,而不是寫入檔案系統。 |
參數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--schema | 否 | 指定要處理的 schema.prisma 檔案路徑,而非使用預設路徑。支援絕對路徑與相對路徑。 | ./schema.prisma, ./prisma/schema.prisma |
範例
分析資料庫並將其 schema 寫入 schema.prisma 檔案
prisma db pull
Introspecting based on datasource defined in schema.prisma …
✔ Introspected 2 models and wrote them into schema.prisma in 38ms
Run prisma generate to generate Prisma Client.
指定另一個 schema.prisma 檔案進行讀寫
prisma db pull --schema=./alternative/schema.prisma
Introspecting based on datasource defined in alternative/schema.prisma …
✔ Introspected 2 models and wrote them into alternative/schema.prisma in 60ms
Run prisma generate to generate Prisma Client.
顯示產生的 schema.prisma 檔案,而不寫入檔案系統
prisma db pull --print
generator client {
provider = "prisma-client"
output = "./generated"
}
datasource db {
provider = "sqlite"
url = "file:./hello-prisma.db"
}
model User {
email String @unique
name String?
user_id Int @id @default(autoincrement())
post Post[]
profile Profile[]
}
model Post {
content String?
post_id Int @id @default(autoincrement())
title String
author User? @relation(fields: [author_id], references: [user_id])
author_id Int?
}
model Profile {
bio String?
profile_id Int @id @default(autoincrement())
user User @relation(fields: [user_id], references: [user_id])
user_id Int @unique
}
db push
db push 指令在不使用遷移 (migrations) 的情況下,將 Prisma schema 的狀態推送到資料庫。如果資料庫不存在,它會建立資料庫。
當您不需要對 schema 更改進行版本控制時(例如在原型設計和本地開發期間),此指令是一個不錯的選擇。
另請參閱
先決條件
在使用 db push 指令之前,您必須在 prisma.config.ts 檔案中設定資料庫連接。
例如
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "sqlite"
}
import { defineConfig, env } from 'prisma/config'
export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
})
選項
| 選項 | 必填 | 描述 |
|---|---|---|
--skip-generate | 否 | 跳過資產(如 Prisma Client)的產生 |
--force-reset | 否 | 重設資料庫,然後更新 schema - 如果您因無法執行的遷移而需要從頭開始,這非常有用。 |
--accept-data-loss | 否 | 忽略資料遺失警告。如果執行 schema 更改可能導致資料遺失,則必須使用此選項。 |
--help / --h | 否 | 顯示說明訊息 |
參數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--schema | 否 | 指定要處理的 schema.prisma 檔案路徑,而非使用預設路徑。支援絕對路徑與相對路徑。 | ./schema.prisma./prisma/schema.prisma |
範例
推送 schema
prisma db push
推送 schema,並接受資料遺失
prisma db push --accept-data-loss
指定自定義 schema 位置並推送
prisma db push --schema=/tmp/schema.prisma
db seed
db seed 在 3.0.1 版本中從預覽版 (Preview) 轉為正式發布 (GA)。
請參閱 資料庫填充 (Seeding)
選項
| 選項 | 必填 | 描述 |
|---|---|---|
--help / --h | 否 | 顯示說明訊息 |
-- | 否 | 允許使用在種子檔案中定義的自定義參數 |
-- 參數/ 分隔符號/ 雙連字號自 4.15.0 或更高版本起可用。
範例
prisma db seed
db execute
db execute 指令在 3.13.0 及更高版本中為正式發布 (GA)。如果您使用的是 3.9.0 到 3.13.0 之間的版本,它位於 --preview-feature CLI 旗標之後。
目前 MongoDB 不支援此指令。
此指令在不與 Prisma 遷移表互動的情況下,將 SQL 腳本應用於資料庫。該腳本需要兩個輸入
- SQL 腳本,可透過標準輸入 (stdin) 或檔案提供
- 資料來源,可以是資料來源的 URL 或 Prisma schema 檔案的路徑
該指令的輸出與連接器相關,目的不是返回資料,而僅是報告成功或失敗。
另請參閱
先決條件
在使用 db execute 指令之前,如果您不使用 --url 選項,則必須在 prisma.config.ts 檔案中設定資料庫連接。
例如
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "sqlite"
}
您的 prisma.config.ts 檔案應如下所示
import { defineConfig, env } from 'prisma/config'
export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
})
選項
需要以下資料來源輸入之一
| 選項 | 描述 |
|---|---|
--url | 要執行指令的資料來源 URL |
--schema | Prisma schema 檔案的路徑,使用 datasource 區塊中的 URL |
需要以下腳本輸入之一
| 選項 | 描述 |
|---|---|
--stdin | 使用終端機標準輸入作為要執行的腳本 |
--file | 檔案路徑。內容將作為要執行的腳本發送 |
其他選項
| 選項 | 必填 | 描述 |
|---|---|---|
--help | 否 | 顯示說明訊息。 |
範例
-
讀取位於
./script.sql的 SQL 檔案內容,並在schema.prisma檔案的datasource區塊指定的資料庫上執行prisma db execute --file ./script.sql --schema schema.prisma -
從標準輸入獲取 SQL 腳本,並在
DATABASE_URL環境變數指定的資料來源 URL 資料庫上執行echo 'TRUNCATE TABLE dev;' | prisma db execute --stdin --url="$DATABASE_URL"
Prisma Migrate
Prisma Migrate 在 2.19.0 版本中從預覽版轉為正式發布 (GA)。
migrate dev
僅供開發環境使用,需要影子資料庫 (shadow database)
migrate dev 指令會
- 在 影子資料庫 中重新執行現有的遷移歷史,以偵測 schema 偏移 (drift)(編輯或刪除的遷移檔案,或對資料庫結構進行的手動更改)
- 將待處理的遷移應用於影子資料庫(例如同事建立的新遷移)
- 根據您在執行
migrate dev之前對 Prisma schema 所做的任何更改產生新的遷移 - 將所有未套用的遷移套用於開發資料庫,並更新
_prisma_migrations表 - 觸發資產的產生(例如 Prisma Client)
另請參閱
選項
| 選項 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--create-only | 否 | 僅建立新遷移但不套用。如果您未對 schema 進行任何更改,這也有效(在這種情況下,會建立一個空的遷移檔案)。執行 migrate dev 來套用遷移。 | |
--skip-seed | 否 | 跳過觸發種子填充 (seed) | |
--skip-generate | 否 | 跳過觸發產生器(例如 Prisma Client) | |
--name / -n | 否 | 為遷移命名(例如 prisma migrate dev --name added_job_title) | |
--help / -h | 否 | 顯示說明訊息 |
如果在搭配 --create-only 執行 prisma migrate dev 時偵測到 schema 偏移,系統會提示您重設資料庫。
參數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--name | 否 | 遷移的名稱。如果未提供名稱,CLI 將提示您輸入。 | |
--schema | 否 | 指定要處理的 schema.prisma 檔案路徑,而非使用預設路徑。支援絕對路徑與相對路徑。 | ./schema.prisma./prisma/schema.prisma |
範例
套用所有遷移,然後建立並套用任何新遷移
prisma migrate dev
如果 schema 有變更,則套用所有遷移並建立新遷移,但不套用它
prisma migrate dev --create-only
migrate reset
僅供開發環境使用
此命令會
- 如果可能,則刪除資料庫/結構,或者在環境不允許刪除資料庫/結構時執行軟重設 (soft reset)
- 如果資料庫/結構被刪除,則建立一個同名的新資料庫/結構
- 套用所有遷移
- 執行種子腳本
選項
| 選項 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--force | 否 | 跳過確認提示 | |
--skip-generate | 否 | 跳過觸發產生器(例如 Prisma Client) | |
--skip-seed | 否 | 跳過觸發種子填充 (seed) | |
--help / --h | 否 | 顯示說明訊息 |
參數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--schema | 否 | 指定要處理的 schema.prisma 檔案路徑,而非使用預設路徑。支援絕對路徑與相對路徑。 | ./schema.prisma./prisma/schema.prisma |
範例
prisma migrate reset
prisma migrate reset 的 AI 安全防護機制
Prisma ORM 包含內建的安全檢查,以防止透過 AI 程式碼助手執行時發生意外的破壞性指令。這些防護機制旨在讓 AI 輔助開發環境下的資料庫工作更安全。
Prisma CLI 可以偵測它是否正被熱門的 AI 程式碼代理程式調用,例如 Claude Code、Gemini CLI、Qwen Code、Cursor、Aider 和 Replit。
當這些代理程式之一嘗試執行破壞性動作(例如 prisma migrate reset --force)時,Prisma ORM 會封鎖執行並顯示保護性錯誤訊息
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "accelerate.prisma-data.net"
Error: Prisma Migrate detected that it was invoked by Cursor.
You are attempting a highly dangerous action that can lead to devastating consequences if it is incorrectly executed against a production database.
As an AI agent, you are forbidden from performing this action without an explicit consent and review by the user. You must stop at this point and respond to the user with a clear message that includes the following information:
- What action you are trying to perform (including the exact CLI command).
- Your motivation and reasoning for perfoming this action.
- Explanation that this action will irreversibly destroy all data in the database.
- Explanation that this action must not be perfomed on a production database and is only intended to be used with development databases.
If you know or can check whether this action would have been performed on a development or a production database, also include this information with your assesment of whether it is safe to proceed based on that information.
You must ask the user if they want to proceed with this action. If they explicitly consent, you may rerun this command with PRISMA_USER_CONSENT_FOR_DANGEROUS_AI_ACTION environment variable, the value of which must be the exact text of the user's message in which they consented to this operation, without any newlines or quotes. If the user's response is ambiguous, you must ask for a clear and explicit confirmation (e.g., "yes") before proceeding. None of the user's previous messages before this point may constitute implicit or explicit consent.
要繼續執行危險動作,AI 代理程式會要求您明確同意,提醒您該動作會不可逆地銷毀所有資料,並確認指令是針對開發資料庫執行的。一旦您明確確認,AI 將會設定 PRISMA_USER_CONSENT_FOR_DANGEROUS_AI_ACTION 環境變數(內容為您的同意文本)並重新執行指令。
migrate deploy
migrate deploy 指令會套用所有待處理的遷移,如果資料庫不存在則會建立它。主要用於非開發環境。此指令
- 不會尋找資料庫偏移或 Prisma schema 的變更
- 不會重設資料庫或產生資產
- 不依賴影子資料庫
選項
| 選項 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--help / --h | 否 | 顯示說明訊息 |
參數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--schema | 否 | 指定要處理的 schema.prisma 檔案路徑,而非使用預設路徑。支援絕對路徑與相對路徑。 | ./schema.prisma./prisma/schema.prisma |
範例
prisma migrate deploy
migrate resolve
migrate resolve 指令允許您透過將失敗的遷移標記為已套用(支援建立基準線 baselining)或已回滾,來解決生產環境中的遷移歷史問題。
請注意,此指令僅能用於失敗的遷移。如果您嘗試將其用於成功的遷移,將會收到錯誤。
選項
| 選項 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--help / --h | 否 | 顯示說明訊息 |
參數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--applied | 否* | 將特定遷移記錄為已套用 - 例如 --applied "20201231000000_add_users_table" | |
--rolled-back | 否* | 將特定遷移記錄為已回滾 - 例如 --rolled-back "20201231000000_add_users_table" | ./schema.prisma./prisma/schema.prisma |
--schema | 否 | 指定要處理的 schema.prisma 檔案路徑,而非使用預設路徑。支援絕對路徑與相對路徑。 | ./schema.prisma./prisma/schema.prisma |
您必須指定 --rolled-back 或 --applied 其中之一。
範例
prisma migrate resolve --applied 20201231000000_add_users_table
prisma migrate resolve --rolled-back 20201231000000_add_users_table
migrate status
prisma migrate status 指令會查找 ./prisma/migrations/* 資料夾中的遷移檔案以及 _prisma_migrations 表中的條目,並彙整有關資料庫中遷移狀態的資訊。
例如
Status
3 migrations found in prisma/migrations
Your local migration history and the migrations table from your database are different:
The last common migration is: 20201127134938_new_migration
The migration have not yet been applied:
20201208100950_test_migration
The migrations from the database are not found locally in prisma/migrations:
20201208100950_new_migration
在 4.3.0 及更高版本中,prisma migrate status 在以下情況下會以結束代碼 1 退出:
- 發生資料庫連接錯誤
migrations目錄中有尚未套用到資料庫的遷移檔案migrations目錄中的遷移歷史與資料庫狀態不一致 (diverged)- 找不到遷移表
- 發現失敗的遷移
選項
| 選項 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--help / --h | 否 | 顯示說明訊息 |
參數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--schema | 否 | 指定要處理的 schema.prisma 檔案路徑,而非使用預設路徑。支援絕對路徑與相對路徑。 | ./schema.prisma./prisma/schema.prisma |
範例
prisma migrate status
migrate diff
此指令僅部分支援 MongoDB。詳情請參閱下方的指令選項。
此指令比較兩個資料庫結構來源,並輸出將第一個狀態遷移到第二個狀態的說明。
輸出可以是人類可讀的摘要(預設)或可執行的腳本。
migrate diff 指令僅能比較 Prisma 支援的資料庫功能。如果兩個資料庫僅在不支援的功能(如視圖 views 或觸發器 triggers)上有所不同,則 migrate diff 不會顯示它們之間的任何差異。
該指令的格式為
prisma migrate diff --from-... <source1> --to-... <source2>
其中 --from-... 和 --to-... 選項根據資料庫結構來源的類型來選擇。支援的來源類型有:
- 線上資料庫 (live databases)
- 遷移歷史 (migration histories)
- Prisma schema 資料模型
- 空結構
兩個結構來源必須使用相同的資料庫供應商。例如,不支援將 PostgreSQL 資料來源與 SQLite 資料來源進行比較。
另請參閱
先決條件
在使用 migrate diff 指令時,如果您使用 --from-schema-datasource 或 --to-schema-datasource,則必須在 prisma.config.ts 檔案中設定資料庫連接。
例如
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "sqlite"
}
import { defineConfig, env } from 'prisma/config'
export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
})
選項
需要以下 --from-... 選項之一
| 選項 | 描述 | 註解 |
|---|---|---|
--from-url | 資料來源 URL | |
--from-migrations | Prisma Migrate 遷移目錄的路徑 | MongoDB 不支援 |
--from-schema-datamodel | Prisma schema 檔案路徑,使用其資料模型進行比較 | |
--from-schema-datasource | Prisma schema 檔案路徑,使用其 datasource 區塊中的 URL 進行比較 | |
--from-empty | 假設您要遷移的起始資料模型為空 | |
--from-local-d1 | 本地 D1 執行個體的路徑(了解更多) | 自 5.12.0 起可用 |
需要以下 --to-... 選項之一
| 選項 | 描述 | 註解 |
|---|---|---|
--to-url | 資料來源 URL | |
--to-migrations | Prisma Migrate 遷移目錄的路徑 | MongoDB 不支援 |
--to-schema-datamodel | Prisma schema 檔案路徑,使用其資料模型進行比較 | |
--to-schema-datasource | Prisma schema 檔案路徑,使用其 datasource 區塊中的 URL 進行比較 | |
--to-empty | 假設您要遷移到的目標資料模型為空 | |
--to-local-d1 | 本地 D1 執行個體的路徑(了解更多) | 自 5.12.0 起可用 |
其他選項
| 選項 | 必填 | 描述 | 註解 |
|---|---|---|---|
--shadow-database-url | 否 | 影子資料庫的 URL | 僅在使用 --to-migrations 或 --from-migrations 時需要 |
--script | 否 | 輸出 SQL 腳本而非預設的人類可讀摘要 | MongoDB 不支援 |
-o, --output | 否 | 寫入檔案而非標準輸出 (stdout) | 自 5.12.1 起可用 |
--exit-code | 否 | 更改結束代碼行為,以示出差異是否為空(空:0,錯誤:1,非空:2)。預設行為是成功:0,錯誤:1。 | |
--help | 否 | 顯示說明訊息。 |
範例
-
比較兩個由資料來源 URL 指定的資料庫,並輸出預設的人類可讀摘要
prisma migrate diff \
--from-url "$DATABASE_URL" \
--to-url "postgresql://login:password@localhost:5432/db2" -
將 URL 為
$DATABASE_URL的資料庫狀態與./prisma/migrations目錄中遷移所定義的結構進行比較,並將差異輸出到腳本script.sql中prisma migrate diff \
--from-url "$DATABASE_URL" \
--to-migrations ./prisma/migrations \
--shadow-database-url $SHADOW_DATABASE_URL \
--script > script.sql
Prisma Data Platform
platform (早期存取 Early Access)
自版本 5.10.0 或更高版本起,platform 指令可透過 Prisma CLI 存取 Prisma Data Platform。
- 身份驗證:
platform auth login:打開瀏覽器視窗進行登入或建立帳號。platform auth logout:登出平台。platform auth show:顯示目前已驗證使用者的資訊。
- 工作區管理:
platform workspace show:列出您帳號下所有可用的工作區。
- 專案管理:
platform project show:列出指定工作區內的所有專案。platform project create:在指定工作區內建立新專案。platform project delete:刪除指定的專案。
- 環境管理:
platform environment show:列出指定專案的所有環境。platform environment create:在指定專案內建立新環境。platform environment delete:刪除指定的環境。
- API 金鑰管理:
platform apikey show:列出指定環境的所有 API 金鑰。platform apikey create:為指定環境建立新的 API 金鑰。platform apikey delete:刪除指定的 API 金鑰。
- Prisma Accelerate:
platform accelerate enable:為指定環境啟用 Prisma Accelerate。platform accelerate disable:為指定環境停用 Prisma Accelerate。
您可以在此處找到包含參數的完整指令列表。
mcp
啟動 Prisma MCP 伺服器。
Studio
studio
studio 指令允許您以互動方式管理資料。它透過啟動一個本地網頁伺服器,並附帶一個根據您專案資料結構和記錄設定的網頁應用程式來實現此功能。
Prisma ORM v7 引入了更穩定版本的 Prisma Studio,具有更佳的效能和現代化的架構。
Prisma Studio 目前支援 PostgreSQL、MySQL 和 SQLite。尚未支援 CockroachDB 和 MongoDB,但可能會在未來的版本中加入。
有關詳細的資料庫支援資訊(包括 SQLite 的要求),請參閱 Prisma Studio 支援的資料庫。
先決條件
在使用 studio 指令之前,您必須在 prisma.config.ts 檔案中設定資料庫連接。
例如
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "sqlite"
}
import { defineConfig, env } from 'prisma/config'
export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
})
選項
studio 指令支援以下選項
| 選項 | 必填 | 描述 | 預設值 |
|---|---|---|---|
-b, --browser | 否 | 自動開啟 Studio 的瀏覽器。 | <您的預設瀏覽器> |
-h, --help | 否 | 顯示所有可用選項並退出 | |
-p, --port | 否 | 啟動 Studio 的埠號。 | 5555 |
--config | 否 | Prisma 設定檔的自定義路徑 | |
--url | 否 | 資料庫連接字串(會覆寫 Prisma 設定中的連接字串) |
參數
| 參數 | 必填 | 描述 | 預設值 |
|---|---|---|---|
--schema | 否 | 指定要處理的 schema.prisma 檔案路徑,而非使用預設路徑。支援絕對路徑與相對路徑。 | ./schema.prisma./prisma/schema.prisma |
範例
在預設埠啟動 Studio 並在新的瀏覽器分頁中開啟
prisma studio
在不同埠啟動 Studio 並在新的瀏覽器分頁中開啟
prisma studio --port 7777
啟動 Studio 並在 Firefox 分頁中開啟
prisma studio --browser firefox
啟動 Studio 但不自動開啟瀏覽器分頁
prisma studio --browser none
使用自定義 Prisma 設定檔啟動 Studio
prisma studio --config=./prisma.config.ts
使用直接資料庫連接字串啟動 Studio
prisma studio --url="postgresql://user:password@localhost:5432/dbname"
package.json 項目選項
schema
可以透過 package.json 檔案中的 prisma.schema 項目指定所需的 schema.prisma 檔案路徑。該路徑定義了 Prisma CLI 在執行任何指令時應使用的檔案。支援絕對路徑與相對路徑。
{
"name": "my-project",
"version": "1.0.0",
"prisma": {
"schema": "./custom-path-to-schema/schema.prisma"
}
}
此功能自 2.7.0 及更高版本起提供。
seed
用於填充資料來源的指令是在 package.json 檔案中的 prisma.seed 項目裡指定的。當呼叫或觸發 prisma db seed 時會使用它。
請參閱 資料庫填充 (Seeding)
{
"name": "my-project",
"version": "1.0.0",
"prisma": {
"seed": "node ./prisma/seed.js"
}
}
此功能自 3.0.1 及更高版本起提供。
為 CLI 使用 HTTP 代理伺服器
Prisma CLI 支援 自定義 HTTP 代理伺服器。這在位於公司防火牆後時特別有用。
要啟用代理伺服器的使用,請提供以下任一環境變數
HTTP_PROXY或http_proxy:HTTP 流量的代理伺服器 URL,例如https://:8080HTTPS_PROXY或https_proxy:HTTPS 流量的代理伺服器 URL,例如https://:8080
npx create-db
create-db 指令只需一個指令即可配置一個臨時的 Prisma Postgres 資料庫。這是一個獨立的實用工具,可以使用 npx 調用。它是快速測試、原型設計或與 Prisma Postgres 整合的理想選擇。
您可以執行以下變體
| 指令 | 描述 |
|---|---|
npx create-db@latest | 建立一個臨時的 Prisma Postgres 資料庫。 |
npx create-pg@latest | npx create-db 的別名。 |
npx create-postgres@latest | npx create-db 的別名。 |
使用這些指令建立的每個資料庫
- 預設可用時間為 24 小時。
- 可以使用 CLI 輸出中顯示的 URL 免費領取,使其變為永久資料庫。
有關完整的用法詳情、選項(例如 --region 和 --interactive)和範例,請參閱說明文件。