Skip to main content

Prisma CLI 参考

本文档介绍 Prisma CLI 命令、参数和选项。

¥This document describes the Prisma CLI commands, arguments, and options.

命令

¥Commands

version(-v)

version 命令输出有关当前 prisma 版本、平台和引擎二进制文件的信息。

¥The version command outputs information about your current prisma version, platform, and engine binaries.

选项

¥Options

version 命令可识别以下选项来修改其行为:

¥The version command recognizes the following options to modify its behavior:

选项必需的描述
--json以 JSON 格式输发布本信息。

示例

¥Examples

输发布本信息

¥Output version information

prisma version
Show CLI results
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

¥Output version information (-v)

prisma -v
Show CLI results
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

¥Output version information as JSON

prisma version --json
Show CLI results
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 项目。

¥Bootstraps a fresh Prisma ORM project within the current directory.

init 命令不解释任何现有文件。相反,它会在当前目录中创建一个 prisma 目录,其中包含一个基本的 schema.prisma 文件。

¥The init command does not interpret any existing files. Instead, it creates a prisma directory containing a bare-bones schema.prisma file within your current directory.

默认情况下,项目会设置一个 本地 Prisma Postgres 实例,但你可以使用 --datasource-provider 选项选择其他数据库。

¥By default, the project sets up a local Prisma Postgres instance but you can choose a different database using the --datasource-provider option.

参数

¥Arguments

争论必需的描述默认
--datasource-provider指定 datasource 块中 provider 字段的值。选项为 prisma+postgressqlitepostgresqlmysqlsqlservermongodbcockroachdbpostgresql
--db--datasource-provider prisma+postgres 的简写语法;创建一个新的 Prisma Postgres 实例。需要在 中进行身份验证。
--prompt(或 --vibe根据提示构建 Prisma 模式并将其部署到新的 Prisma Postgres 实例。需要在 中进行身份验证。
--url定义自定义 datasource 网址。
--generator-provider定义要使用的生成器提供程序。prisma-client-js
--preview-feature定义要使用的 预览功能。要定义多个预览功能,你必须为每个预览功能多次提供标志。查看示例。
--output指定 生成的客户端的输出位置node_modules/.prisma/client
--with-model将简单的 User 模型添加到初始 Prisma 模式。自 5.14.0 版本起可用。

示例

¥Examples

运行 prisma init

¥Run prisma init

prisma init
Show CLI results
✔ Your Prisma schema was created at prisma/schema.prisma.
You can now open it in your favorite editor.

Next steps:
1. Set the DATABASE_URL in the .env file to point to your existing database. If your database has no tables yet, read https://pris.ly/d/getting-started
2. Set the provider of the datasource block in schema.prisma to match your database: postgresql, mysql, sqlite, sqlserver, mongodb or cockroachdb.
3. Run prisma db pull to turn your database schema into a Prisma schema.
4. Run prisma generate to generate Prisma Client. You can then start querying your database.

More information in our documentation:
https://pris.ly/d/getting-started

接下来,运行 prisma dev 命令与本地 Prisma Postgres 实例交互(例如,运行迁移或执行查询)。

¥Next, run the prisma dev command to interact with your local Prisma Postgres instance (e.g. to run migrations or execute queries).

运行 prisma init --datasource-provider sqlite

¥Run prisma init --datasource-provider sqlite

prisma init --datasource-provider sqlite

命令输出包含有关如何使用生成的文件以及如何开始在项目中使用 Prisma ORM 的有用信息。

¥The command output contains helpful information on how to use the generated files and begin using Prisma ORM with your project.

运行 prisma init --db

¥Run prisma init --db

prisma init --db

该命令创建一个新的 Prisma Postgres 实例。请注意,它要求你使用 进行身份验证,如果你第一次运行它时没有进行身份验证,该命令将打开浏览器供你登录控制台。

¥The command creates a new Prisma Postgres instance. Note that it requires you to be authenticated with the , If you run it for the first time without being authenticated, the command will open the browser for you to log into Console.

运行 prisma init --prompt "Simple habit tracker application"

¥Run prisma init --prompt "Simple habit tracker application"

prisma init --prompt "Simple habit tracker application"

该命令搭建 Prisma 模式并将其部署到全新的 Prisma Postgres 实例。请注意,它要求你使用 进行身份验证,如果你第一次运行它时没有进行身份验证,该命令将打开浏览器供你登录控制台。

¥The command scaffolds a Prisma schema and deploys it to a fresh Prisma Postgres instance. Note that it requires you to be authenticated with the , If you run it for the first time without being authenticated, the command will open the browser for you to log into Console.

运行 prisma init --preview-feature

¥Run prisma init --preview-feature

prisma init --preview-feature multiSchema
Show Prisma schema results
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
previewFeatures = ["multiSchema"]
}
prisma init --preview-feature multiSchema --preview-feature metrics
Show Prisma schema results
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
previewFeatures = ["multiSchema", "metrics"]
}

生成的资源

¥Generated Assets

prisma/schema.prisma

用于定义架构的初始 schema.prisma 文件:

¥An initial schema.prisma file to define your schema in:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
output = "../generated/prisma"
}

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

.env

用于定义项目环境变量的文件:

¥A file to define environment variables for your project:

# 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="file:./dev.db"

.gitignore

一个文件,用于指定 Git 应忽略项目中的哪些文件夹/文件。

¥A file to specify what folders/files git should ignore in your project.

node_modules
# Keep environment variables out of version control
.env

/generated/prisma

运行 prisma init --url mysql://user:password@localhost:3306/mydb

¥Run prisma init --url mysql://user:password@localhost:3306/mydb

带有 --url 参数的 init 命令允许你在 Prisma 初始化期间指定自定义数据源 URL,而不是依赖占位符数据库 URL:

¥The init command with the --url argument allows you to specify a custom datasource URL during Prisma initialization, instead of relying on a placeholder database URL:

prisma init --url mysql://user:password@localhost:3306/mydb

生成的资源

¥Generated Assets

prisma/schema.prisma

用于定义架构的最小 schema.prisma 文件:

¥A minimal schema.prisma file to define your schema in:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

generator client {
provider = "prisma-client-js"
}

.env

用于定义项目环境变量的文件:

¥A file to define environment variables for your project:

# 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"

dev

dev 命令启动一个 本地 Prisma Postgres 数据库,你可以在该数据库上运行 Prisma ORM 命令。它对于开发和测试非常有用,并且还允许你在生产环境中轻松切换到 Prisma Postgres。Y

¥The dev command starts a local Prisma Postgres database that you can run Prisma ORM commands against. It is useful for development and testing purposes and also allows you to switch to Prisma Postgres in production easily. Y

参数

¥Arguments

争论必需的描述默认
--name(或 -n启用针对特定数据库实例的定位。了解更多
--port(或 -p本地 Prisma Postgres HTTP 服务器将监听的主端口号。51213
--db-port(或 -P本地 Prisma Postgres 数据库服务器将监听的端口号。51214
--shadow-db-port影子数据库服务器将监听的端口号。51215
--debug启用调试日志记录。false

示例

¥Examples

运行 prisma dev

¥Run prisma dev

prisma dev
Show CLI results
$ 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│
╰──────────────────────────────╯

generate

generate 命令根据 prisma/schema.prisma 文件中定义的 generatordata model 块生成 Prisma Client 等资源。

¥The generate command generates assets like Prisma Client based on the generator and data model blocks defined in your prisma/schema.prisma file.

generate 命令最常用于使用 prisma-client-js 生成器生成 Prisma 客户端。这做了三件事:

¥The generate command is most often used to generate Prisma Client with the prisma-client-js generator. This does three things:

  1. 搜索当前目录和父目录以查找适用的 npm 项目。如果找不到,它将在当前目录中创建一个 package.json 文件。

    ¥Searches the current directory and parent directories to find the applicable npm project. It will create a package.json file in the current directory if it cannot find one.

  2. 如果 @prisma/client 项目尚不存在,则将其安装到 npm 项目中。

    ¥Installs the @prisma/client into the npm project if it is not already present.

  3. 检查当前目录以查找要处理的 Prisma 模式。然后它将为你的项目生成一个定制的 Prisma 客户端

    ¥Inspects the current directory to find a Prisma Schema to process. It will then generate a customized Prisma Client for your project.

先决条件

¥Prerequisites

要使用 generate 命令,你必须在 schema.prisma 文件中添加生成器定义。可以通过在 schema.prisma 文件中包含以下内容来添加用于生成 Prisma 客户端的 prisma-client-js 生成器:

¥To use the generate command, you must add a generator definition in your schema.prisma file. The prisma-client-js generator, used to generate Prisma Client, can be added by including the following in your schema.prisma file:

generator client {
provider = "prisma-client-js"
}

选项

¥Options

选项必需的描述默认
--data-proxygenerate 命令将生成 Prisma 客户端,以便与 Prisma 5.0.0 之前的 Prisma 加速 一起使用。与 --accelerate--no-engine 互斥。
--accelerategenerate 命令将生成与 Prisma 加速 一起使用的 Prisma 客户端。与 --data-proxy--no-engine 互斥。适用于 Prisma 5.1.0 及更高版本。
--no-enginegenerate 命令将生成 Prisma 客户端,无需与 Prisma 加速 一起使用的随附引擎。与 --data-proxy--accelerate 互斥。适用于 Prisma ORM 5.2.0 及更高版本。
--no-hintsgenerate 命令将生成 Prisma 客户端,但不会在终端打印使用提示、调查问卷或信息横幅。在 Prisma ORM 5.16.0 及更高版本中可用。
--allow-no-modelsgenerate 命令将生成 Prisma Client 而不生成任何模型。
--watchgenerate 命令将继续监视 schema.prisma 文件并在文件更改时重新生成 Prisma 客户端。
warning

弃用警告

¥Deprecation Warning

从 Prisma 5.2.0 开始,--data-proxy--accelerate 已被弃用,取而代之的是 --no-engine,因为 Prisma 客户端不再需要与 Prisma Accelerate 配合使用的选项。所有选项均可用且工作方式类似,但我们推荐 --no-engine,因为它可以防止下载引擎,这将极大地影响部署到无服务器和边缘功能的应用的大小。

¥As of Prisma 5.2.0, --data-proxy and --accelerate are deprecated in favor of --no-engine as Prisma Client no longer requires an option to work with Prisma Accelerate. All options are available and work similarly, but we recommend --no-engine as it prevents an engine from being downloaded which will greatly impact the size of apps deployed to serverless and edge functions.

参数

¥Arguments

争论必需的描述默认
--schema指定要处理的所需 schema.prisma 文件的路径,而不是默认路径。支持绝对路径和相对路径。./schema.prisma./prisma/schema.prisma
--generator指定使用哪个生成器来生成资源。可以多次提供此选项以包括多个生成器。默认情况下,目标模式中的所有生成器都将运行。

示例

¥Examples

使用默认 schema.prisma 路径生成 Prisma 客户端

¥Generate Prisma Client using the default schema.prisma path

prisma generate
Show CLI results
✔ Generated Prisma Client to ./node_modules/.prisma/client in 61ms

You can now start using Prisma Client in your code:

import { PrismaClient } from '@prisma/client'
// or const { PrismaClient } = require('@prisma/client')

const prisma = new PrismaClient()

Explore the full API: https://pris.ly/d/client
使用非默认 schema.prisma 路径生成 Prisma 客户端

¥Generate Prisma Client using a non-default schema.prisma path

prisma generate --schema=./alternative/schema.prisma
继续观察 schema.prisma 文件的更改以自动重新生成 Prisma 客户端

¥Continue watching the schema.prisma file for changes to automatically re-generate Prisma Client

prisma generate --watch
Show CLI results
Watching... /home/prismauser/prisma/prisma-play/prisma/schema.prisma

✔ Generated Prisma Client to ./node_modules/.prisma/client in 45ms
仅使用特定生成器运行 generate 命令

¥Run the generate command with only a specific generator

prisma generate --generator client
使用多个特定生成器运行 generate 命令

¥Run the generate command with multiple specific generators

prisma generate --generator client --generator zod_schemas

生成的资源

¥Generated Assets

默认情况下,prisma-client-js 生成器会创建一个自定义客户端,用于处理 ./node_modules/.prisma/client 目录中的数据库 - 你可以 自定义输出文件夹

¥The prisma-client-js generator creates a customized client for working with your database within the ./node_modules/.prisma/client directory by default - you can customize the output folder.

validate

验证 Prisma 架构文件的 Prisma 模式语言

¥Validates the Prisma Schema Language of the Prisma schema file.

参数

¥Arguments

争论必需的描述默认
--schema指定要处理的所需 schema.prisma 文件的路径,而不是默认路径。支持绝对路径和相对路径。./schema.prisma./prisma/schema.prisma

示例

¥Examples

验证架构没有错误

¥Validate a schema without errors

prisma validate
Show CLI results
验证存在验证错误的架构

¥Validate a schema with validation errors

prisma validate
Show CLI results

format

格式化 Prisma 架构文件,其中包括验证、格式化和保留架构。

¥Formats the Prisma schema file, which includes validating, formatting, and persisting the schema.

参数

¥Arguments

争论必需的描述默认
--schema指定要处理的所需 schema.prisma 文件的路径,而不是默认路径。支持绝对路径和相对路径。./schema.prisma./prisma/schema.prisma
--check如果任何文件未格式化,则会失败。这可以在 CI 中用于检测架构格式是否正确

示例

¥Examples

验证架构没有错误

¥Validate a schema without errors

prisma format
Show CLI results
格式化包含验证错误的架构

¥Formatting a schema with validation errors

prisma format
Show CLI results

debug

打印调试和错误报告的信息。

¥Prints information for debugging and bug reports.

信息

从版本 5.6.0 及更高版本开始可以使用此功能。

¥This is available from version 5.6.0 and newer.

参数

¥Arguments

争论必需的描述默认
--schema指定要处理的所需 schema.prisma 文件的路径,而不是默认路径。支持绝对路径和相对路径。./schema.prisma./prisma/schema.prisma
--help/--h显示帮助消息

示例

¥Example

prisma debug
Show CLI results

如果你使用的是旧版本的 Prisma,你可以通过运行以下命令来使用此命令:

¥If you're using an older version of Prisma, you can use this command by running:

npx prisma@latest debug

db

db pull

db pull 命令连接到你的数据库并将 Prisma 模型添加到反映当前数据库架构的 Prisma 架构中。

¥The db pull command connects to your database and adds Prisma models to your Prisma schema that reflect the current database schema.

warning

警告:该命令将使用新架构覆盖当前 schema.prisma 文件。一些手动更改或自定义可能会丢失。如果当前的 schema.prisma 文件包含重要修改,请务必在运行 db pull 之前备份当前的 schema.prisma 文件(或将当前状态提交给版本控制以便能够恢复任何更改)。

¥Warning: The command will overwrite the current schema.prisma file with the new schema. Some manual changes or customization can be lost. Be sure to back up your current schema.prisma file (or commit your current state to version control to be able to revert any changes) before running db pull if it contains important modifications.

info

MongoDB 连接器 上使用 db pull 命令进行内省会对数据进行采样,而不是读取架构。

¥Introspection with the db pull command on the MongoDB connector samples the data instead of reading a schema.

先决条件

¥Prerequisites

在使用 db pull 命令之前,你必须在 schema.prisma 文件中定义有效的 datasource

¥Before using the db pull command, you must define a valid datasource within your schema.prisma file.

例如,以下 datasource 在当前目录中定义了一个 SQLite 数据库文件:

¥For example, the following datasource defines a SQLite database file within the current directory:

datasource db {
provider = "sqlite"
url = "file:my-database.db"
}

选项

¥Options

选项必需的描述默认
--force强制覆盖对架构所做的手动更改。生成的模式将仅基于内省模式。
--print将创建的 schema.prisma 打印到屏幕上,而不是将其写入文件系统。

参数

¥Arguments

争论必需的描述默认
--schema指定要处理的所需 schema.prisma 文件的路径,而不是默认路径。支持绝对路径和相对路径。./schema.prisma./prisma/schema.prisma

示例

¥Examples

分析数据库并将其架构写入 schema.prisma 文件

¥Analyze the database and write its schema to the schema.prisma file

prisma db pull
Show CLI results
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 文件

¥Specify an alternative schema.prisma file to read and write to

prisma db pull --schema=./alternative/schema.prisma
Show CLI results
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 文件而不是将其写入文件系统

¥Display the generated schema.prisma file instead of writing it to the filesystem

prisma db pull --print
Show CLI results
generator client {
provider = "prisma-client-js"
}

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 命令将 Prisma 模式的状态推送到数据库,而无需使用迁移。如果数据库不存在,它将创建数据库。

¥The db push command pushes the state of your Prisma schema to the database without using migrations. It creates the database if the database does not exist.

当你不需要对架构更改进行版本控制时(例如在原型设计和本地开发期间),此命令是一个不错的选择。

¥This command is a good choice when you do not need to version schema changes, such as during prototyping and local development.

也可以看看:

¥See also:

先决条件

¥Prerequisites

在使用 db push 命令之前,你必须在 schema.prisma 文件中定义有效的 datasource

¥Before using the db push command, you must define a valid datasource within your schema.prisma file.

例如,以下 datasource 定义当前目录中的 SQLite 数据库文件:

¥For example, the following datasource defines a SQLite database file within the current directory:

datasource db {
provider = "sqlite"
url = "file:my-database.db"
}

选项

¥Options

选项必需的描述
--skip-generate跳过 Prisma Client 等工件的生成
--force-reset重置数据库,然后更新架构 - 如果由于无法执行的迁移而需要从头开始,则非常有用。
--accept-data-loss忽略数据丢失警告。如果由于架构更改而导致数据可能丢失,则需要此选项。
--help/--h显示帮助消息

参数

¥Arguments

争论必需的描述默认
--schema指定要处理的所需 schema.prisma 文件的路径,而不是默认路径。支持绝对路径和相对路径。./schema.prisma
./prisma/schema.prisma

示例

¥Examples

推送架构:

¥Push the schema:

prisma db push

推送架构,接受数据丢失:

¥Push the schema, accepting data loss:

prisma db push --accept-data-loss

使用自定义架构位置推送架构:

¥Push the schema with a custom schema location:

prisma db push --schema=/tmp/schema.prisma

db seed

db seed 在 3.0.1 中从预览版更改为正式版 (GA)。

¥db seed changed from Preview to Generally Available (GA) in 3.0.1.

为你的数据库播种

¥See Seeding your database

选项

¥Options

选项必需的描述
--help/--h显示帮助消息
--允许使用种子文件中定义的自定义参数

-- 参数/delimiter/双破折号在 4.15.0 或更高版本中可用。

¥The -- argument/ delimiter/ double-dash is available from version 4.15.0 or later.

示例

¥Examples

prisma db seed

db execute

info

db execute 命令在 3.13.0 及更高版本中普遍可用。如果你使用的是 3.9.0 和 3.13.0 之间的版本,则可以在 --preview-feature CLI 标志后面使用它。

¥The db execute command is Generally Available in versions 3.13.0 and later. If you're using a version between 3.9.0 and 3.13.0, it is available behind a --preview-feature CLI flag.

warning

MongoDB 目前不支持该命令。

¥This command is currently not supported on MongoDB.

此命令将 SQL 脚本应用于数据库,而不与 Prisma 迁移表交互。该脚本需要两个输入:

¥This command applies a SQL script to the database without interacting with the Prisma migrations table. The script takes two inputs:

  • SQL 脚本,可以在标准输入或文件中提供

    ¥the SQL script, which can be provided either on standard input or in a file

  • 数据源,可以是数据源的 URL 或 Prisma 架构文件的路径

    ¥the data source, which can either be the URL of the data source or the path to your Prisma schema file

该命令的输出是特定于连接器的,并不用于返回数据,而仅用于报告成功或失败。

¥The output of the command is connector-specific, and is not meant for returning data, but only to report success or failure.

也可以看看:

¥See also:

先决条件

¥Prerequisites

在使用 db execute 命令之前,如果不使用 --url 选项,则必须在 schema.prisma 文件中定义有效的 datasource

¥Before using the db execute command, if you do not use the --url option you must define a valid datasource within your schema.prisma file.

例如,以下 datasource 在当前目录中定义了一个 SQLite 数据库文件:

¥For example, the following datasource defines a SQLite database file within the current directory:

datasource db {
provider = "sqlite"
url = "file:my-database.db"
}

选项

¥Options

需要以下数据源输入之一:

¥One of the following data source inputs is required:

选项描述
--url运行命令的数据源的 URL
--schemaPrisma 模式文件的路径,使用 datasource 块中的 URL

需要以下脚本输入之一:

¥One of the following script inputs is required:

选项描述
--stdin使用终端标准输入作为要执行的脚本
--file文件的路径。内容将作为要执行的脚本发送

其他选项:

¥Other options:

选项必需的描述
--help显示帮助消息。

示例

¥Examples

  • 获取位于 ./script.sql 的 SQL 文件的内容,并在 schema.prisma 文件的 datasource 块中的 URL 指定的数据库上执行它:

    ¥Take the content of a SQL file located at ./script.sql and execute it on the database specified by the URL in the datasource block of your schema.prisma file:

    prisma db execute --file ./script.sql --schema schema.prisma
  • 从标准输入获取 SQL 脚本,并在 DATABASE_URL 环境变量中给定的数据源 URL 指定的数据库上执行它:

    ¥Take the SQL script from standard input and execute it on the database specified by the data source URL given in the DATABASE_URL environment variable:

    echo 'TRUNCATE TABLE dev;' | prisma db execute --stdin --url="$DATABASE_URL"

Prisma 迁移

¥Prisma Migrate

Prisma Migrate 在 2.19.0 中从预览版更改为正式版 (GA)。

¥Prisma Migrate changed from Preview to Generally Available (GA) in 2.19.0.

info

不适用于 MongoDB
MongoDB 使用 db push,而不是 migrate dev 和相关命令。

¥Does not apply for MongoDB
Instead of migrate dev and related commands, db push is used for MongoDB.

migrate dev

仅在开发环境中使用,需要影子数据库

¥For use in development environments only, requires shadow database

migrate dev 命令:

¥The migrate dev command:

  1. 重新运行 影子数据库 中的现有迁移历史记录,以检测架构漂移(编辑或删除迁移文件,或对数据库架构进行手动更改)

    ¥Reruns the existing migration history in the shadow database in order to detect schema drift (edited or deleted migration file, or a manual changes to the database schema)

  2. 将待处理的迁移应用到影子数据库(例如,同事创建的新迁移)

    ¥Applies pending migrations to the shadow database (for example, new migrations created by colleagues)

  3. 根据运行 migrate dev 之前对 Prisma 架构所做的任何更改生成新的迁移

    ¥Generates a new migration from any changes you made to the Prisma schema before running migrate dev

  4. 将所有未应用的迁移应用到开发数据库并更新 _prisma_migrations

    ¥Applies all unapplied migrations to the development database and updates the _prisma_migrations table

  5. 触发工件的生成(例如,Prisma Client)

    ¥Triggers the generation of artifacts (for example, Prisma Client)

warning

MongoDB 不支持该命令。请改用 db push

¥This command is not supported on MongoDB. Use db push instead.

也可以看看:

¥See also:

选项

¥Options

选项必需的描述默认
--create-only创建新的迁移但不应用它。如果你没有对架构进行任何更改(在这种情况下,将创建一个空迁移),这也有效。运行 migrate dev 以应用迁移。
--skip-seed跳过触发种子
--skip-generate跳过触发生成器(例如 Prisma Client)
--name/-n为迁移命名(例如 prisma migrate dev --name added_job_title
--help/-h显示帮助消息
info

如果在使用 --create-only 运行 prisma migrate dev 时检测到 图式漂移,系统将提示你重置数据库。

¥If a schema drift is detected while running prisma migrate dev using --create-only, you will be prompted to reset your database.

参数

¥Arguments

争论必需的描述默认
--name迁移的名称。如果未提供名称,CLI 将提示你。
--schema指定要处理的所需 schema.prisma 文件的路径,而不是默认路径。支持绝对路径和相对路径。./schema.prisma
./prisma/schema.prisma

示例

¥Examples

应用所有迁移,然后创建并应用任何新迁移:

¥Apply all migrations, then create and apply any new migrations:

prisma migrate dev

如果架构发生更改,则应用所有迁移并创建新迁移,但不应用它:

¥Apply all migrations and create a new migration if there are schema changes, but do not apply it:

prisma migrate dev --create-only

migrate reset

仅适用于开发环境

¥For use in development environments only

这个命令:

¥This command:

  1. 如果可能,则删除数据库/架构,或者如果环境不允许删除数据库/架构,则执行软重置

    ¥Drops the database/schema if possible, or performs a soft reset if the environment does not allow deleting databases/schemas

  2. 如果数据库/模式被删除,则创建具有相同名称的新数据库/模式

    ¥Creates a new database/schema with the same name if the database/schema was dropped

  3. 应用所有迁移

    ¥Applies all migrations

  4. 运行种子脚本

    ¥Runs seed scripts

warning

MongoDB 不支持该命令。请改用 db push

¥This command is not supported on MongoDB. Use db push instead.

选项

¥Options

选项必需的描述默认
--force跳过确认提示
--skip-generate跳过触发生成器(例如 Prisma Client)
--skip-seed跳过触发种子
--help/--h显示帮助消息

参数

¥Arguments

争论必需的描述默认
--schema指定要处理的所需 schema.prisma 文件的路径,而不是默认路径。支持绝对路径和相对路径。./schema.prisma
./prisma/schema.prisma

示例

¥Examples

prisma migrate reset

migrate deploy

migrate deploy 命令应用所有挂起的迁移,并创建数据库(如果不存在)。主要用于非开发环境。这个命令:

¥The migrate deploy command applies all pending migrations, and creates the database if it does not exist. Primarily used in non-development environments. This command:

  • 不查找数据库中的偏差或 Prisma 架构中的更改

    ¥Does not look for drift in the database or changes in the Prisma schema

  • 不重置数据库或生成工件

    ¥Does not reset the database or generate artifacts

  • 不依赖影子数据库

    ¥Does not rely on a shadow database

warning

MongoDB 不支持该命令。请改用 db push

¥This command is not supported on MongoDB. Use db push instead.

选项

¥Options

选项必需的描述默认
--help/--h显示帮助消息

参数

¥Arguments

争论必需的描述默认
--schema指定要处理的所需 schema.prisma 文件的路径,而不是默认路径。支持绝对路径和相对路径。./schema.prisma
./prisma/schema.prisma

示例

¥Examples

prisma migrate deploy

migrate resolve

migrate resolve 命令允许你通过将失败的迁移标记为已应用(支持基线)或回滚来解决生产中的迁移历史记录问题。

¥The migrate resolve command allows you to solve migration history issues in production by marking a failed migration as already applied (supports baselining) or rolled back.

请注意,此命令只能在迁移失败时使用。如果你尝试使用它来成功迁移,你将收到错误。

¥Note that this command can only be used with a failed migration. If you try to use it with a successful migration you will receive an error.

warning

MongoDB 不支持该命令。请改用 db push

¥This command is not supported on MongoDB. Use db push instead.

选项

¥Options

选项必需的描述默认
--help/--h显示帮助消息

参数

¥Arguments

争论必需的描述默认
--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

¥You must specify either --rolled-back or --applied.

示例

¥Examples

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 表中的条目,并编译有关数据库中迁移状态的信息。

¥The prisma migrate status command looks up the migrations in ./prisma/migrations/* folder and the entries in the _prisma_migrations table and compiles information about the state of the migrations in your database.

warning

MongoDB 不支持该命令。请改用 db push

¥This command is not supported on MongoDB. Use db push instead.

例如:

¥For example:

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 退出:

¥In versions 4.3.0 and later, prisma migrate status exits with exit code 1 in the following cases:

  • 发生数据库连接错误

    ¥a database connection error occurs

  • migrations 目录下有未应用到数据库的迁移文件

    ¥there are migration files in the migrations directory that have not been applied to the database

  • migrations 目录中的迁移历史记录与数据库的状态不同

    ¥the migration history in the migrations directory has diverged from the state of the database

  • 没有找到迁移表

    ¥no migration table is found

  • 发现失败的迁移

    ¥failed migrations are found

选项

¥Options

选项必需的描述默认
--help/--h显示帮助消息

参数

¥Arguments

争论必需的描述默认
--schema指定要处理的所需 schema.prisma 文件的路径,而不是默认路径。支持绝对路径和相对路径。./schema.prisma
./prisma/schema.prisma

示例

¥Examples

prisma migrate status

migrate diff

信息

MongoDB 仅部分支持此命令。有关详细信息,请参阅下面的命令选项。

¥This command is only partially supported for MongoDB. See the command options below for details.

此命令比较两个数据库模式源,并输出将第一个数据库模式源迁移到第二个数据库模式源的状态的描述。

¥This command compares two database schema sources and outputs a description of a migration taking the first to the state of the second.

输出可以作为人类可读的摘要(默认)或可执行脚本给出。

¥The output can be given either as a human-readable summary (the default) or an executable script.

警告

migrate diff 命令只能比较 由 Prisma 支持 的数据库功能。如果两个数据库仅在不支持的功能(例如视图或触发器)上有所不同,则 migrate diff 将不会显示它们之间的任何差异。

¥The migrate diff command can only compare database features that are supported by Prisma. If two databases differ only in unsupported features, such as views or triggers, then migrate diff will not show any difference between them.

命令的格式为:

¥The format of the command is:

prisma migrate diff --from-... <source1> --to-... <source2>

其中 --from-...--to-... 选项是根据数据库架构源的类型选择的。支持的来源类型有:

¥where the --from-... and --to-... options are selected based on the type of database schema source. The supported types of sources are:

  • 实时数据库

    ¥live databases

  • 迁徙历史

    ¥migration histories

  • Prisma 架构数据模型

    ¥Prisma schema data models

  • 一个空的模式

    ¥an empty schema

两个架构源必须使用相同的数据库提供程序。例如,不支持比较 PostgreSQL 数据源与 SQLite 数据源的差异。

¥Both schema sources must use the same database provider. For example, a diff comparing a PostgreSQL data source with a SQLite data source is not supported.

也可以看看:

¥See also:

先决条件

¥Prerequisites

在使用 migrate diff 命令之前,如果你使用 --from-schema-datasource--to-schema-datasource,则必须在 schema.prisma 文件中定义有效的 datasource

¥Before using the migrate diff command, if you are using the --from-schema-datasource or --to-schema-datasource you must define a valid datasource within your schema.prisma file.

例如,以下 datasource 在当前目录中定义了一个 SQLite 数据库文件:

¥For example, the following datasource defines a SQLite database file within the current directory:

datasource db {
provider = "sqlite"
url = "file:my-database.db"
}

选项

¥Options

需要以下 --from-... 选项之一:

¥One of the following --from-... options is required:

选项描述注意
--from-url数据源 URL
--from-migrationsPrisma Migrate 迁移目录的路径MongoDB 不支持
--from-schema-datamodelPrisma 模式文件的路径,使用差异的数据模型
--from-schema-datasourcePrisma 架构文件的路径,使用 datasource 块中的 URL 进行差异
--from-empty假设你要迁移的数据模型是空的
--from-local-d1本地 D1 实例 (了解更多) 的路径5.12.0 年起可用

需要以下 --to-... 选项之一:

¥One of the following --to-... options is required:

选项描述注意
--to-url数据源 URL
--to-migrationsPrisma Migrate 迁移目录的路径MongoDB 不支持
--to-schema-datamodelPrisma 模式文件的路径,使用差异的数据模型
--to-schema-datasourcePrisma 架构文件的路径,使用 datasource 块中的 URL 进行差异
--to-empty假设你要迁移到的数据模型为空
--to-local-d1本地 D1 实例 (了解更多) 的路径5.12.0 年起可用

其他选项:

¥Other options:

选项必需的描述注意
--shadow-database-url影子数据库的 URL仅当使用 --to-migrations--from-migrations 时才需要
--script输出 SQL 脚本而不是默认的人类可读摘要MongoDB 不支持
-o--output写入文件而不是标准输出5.12.1 年起可用
--exit-code更改退出代码行为以指示 diff 是否为空(空:0、错误:1、非空:2)。默认行为是成功:0,错误:1.
--help显示帮助消息。

示例

¥Examples

  • 比较由数据源 URL 指定的两个数据库,并输出默认的人类可读摘要:

    ¥Compare two databases specified by their data source URL, and output the default human-readable summary:

    prisma migrate diff \
    --from-url "$DATABASE_URL" \
    --to-url "postgresql://login:password@localhost:5432/db2"
  • 将 URL 为 $DATABASE_URL 的数据库的状态与 ./prisma/migrations 目录中的迁移定义的架构进行比较,并将差异输出到脚本 script.sql

    ¥Compare the state of a database with a URL of $DATABASE_URL to the schema defined by the migrations in the ./prisma/migrations directory, and output the differences to a script script.sql:

    prisma migrate diff \
    --from-url "$DATABASE_URL" \
    --to-migrations ./prisma/migrations \
    --shadow-database-url $SHADOW_DATABASE_URL \
    --script > script.sql

Prisma 数据平台

¥Prisma Data Platform

platform(抢先体验)

¥platform (Early Access)

从版本 5.10.0 或更高版本开始,platform 命令提供通过 Prisma CLI 访问 Prisma 数据平台。

¥The platform command provides access to the Prisma Data Platform through the Prisma CLI starting in version 5.10.0 or later.

  • 验证:

    ¥Authentication:

    • platform auth login:打开浏览器窗口用于登录或创建账户。

      ¥platform auth login: Opens a browser window for login or account creation.

    • platform auth logout:退出平台。

      ¥platform auth logout: Logs out of the platform.

    • platform auth show:显示有关当前经过身份验证的用户的信息。

      ¥platform auth show: Displays information about the currently authenticated user.

  • 工作区管理:

    ¥Workspace Management:

    • platform workspace show:列出你账户可用的所有工作区。

      ¥platform workspace show: Lists all workspaces available to your account.

  • 项目管理:

    ¥Project Management:

    • platform project show:列出指定工作区中的所有项目。

      ¥platform project show: Lists all projects within the specified workspace.

    • platform project create:在指定的工作区中创建一个新项目。

      ¥platform project create: Creates a new project within the specified workspace.

    • platform project delete:删除指定的项目。

      ¥platform project delete: Deletes the specified project.

  • 环境管理:

    ¥Environment Management:

    • platform environment show:列出指定项目的所有环境。

      ¥platform environment show: Lists all environments for the specified project.

    • platform environment create:在指定项目中创建新环境。

      ¥platform environment create: Creates a new environment within the specified project.

    • platform environment delete:删除指定的环境。

      ¥platform environment delete: Deletes the specified environment.

  • API 密钥管理:

    ¥API Key Management:

    • platform apikey show:列出指定环境的所有 API 密钥。

      ¥platform apikey show: Lists all API keys for the specified environment.

    • platform apikey create:为指定环境创建新的 API 密钥。

      ¥platform apikey create: Creates a new API key for the specified environment.

    • platform apikey delete:删除指定的 API 密钥。

      ¥platform apikey delete: Deletes the specified API key.

  • Prisma 加速:

    ¥Prisma Accelerate:

    • platform accelerate enable:为指定环境启用 Prisma Accelerate。

      ¥platform accelerate enable: Enables Prisma Accelerate for the specified environment.

    • platform accelerate disable:针对指定环境禁用 Prisma Accelerate。

      ¥platform accelerate disable: Disables Prisma Accelerate for the specified environment.

你可以使用参数 此处 找到可用命令的完整列表。

¥You can find the complete list of available commands with the arguments here.

mcp

启动 Prisma MCP 服务器

¥Starts the Prisma MCP server.

工作室

¥Studio

studio

studio 命令允许你以交互方式与数据交互并管理数据。它通过启动本地 Web 服务器和配置了项目数据架构和记录的 Web 应用来实现此目的。

¥The studio command allows you to interact with and manage your data interactively. It does this by starting a local web server with a web app configured with your project's data schema and records.

先决条件

¥Prerequisites

在使用 studio 命令之前,你必须在 schema.prisma 文件中定义有效的 datasource

¥Before using the studio command, you must define a valid datasource within your schema.prisma file.

例如,以下 datasource 在当前目录中定义了一个 SQLite 数据库文件:

¥For example, the following datasource defines a SQLite database file within the current directory:

datasource db {
provider = "sqlite"
url = "file:my-database.db"
}

选项

¥Options

studio 命令可识别以下选项:

¥The studio command recognizes the following options:

选项必需的描述默认
-b--browser自动打开 Studio 的浏览器。<your-default-browser>
-h--help显示所有可用选项并退出
-p--port启动 Studio 的端口号。5555

参数

¥Arguments

争论必需的描述默认
--schema指定要处理的所需 schema.prisma 文件的路径,而不是默认路径。支持绝对路径和相对路径。./schema.prisma
./prisma/schema.prisma

示例

¥Examples

在默认端口上启动 Studio 并打开一个新的浏览器选项卡

¥Start Studio on the default port and open a new browser tab to it

prisma studio

在不同的端口上启动 Studio 并打开一个新的浏览器选项卡

¥Start Studio on a different port and open a new browser tab to it

prisma studio --port 7777

启动 Studio 并打开 Firefox 选项卡

¥Start Studio and open a Firefox tab to it

prisma studio --browser firefox

启动 Studio,无需打开新的浏览器选项卡

¥Start Studio without opening a new browser tab to it

prisma studio --browser none

package.json 输入选项

¥package.json entry options

schema

可以使用 package.json 文件中的 prisma.schema 条目指定所需 schema.prisma 文件的路径。该路径定义当你运行任何 CLI 命令时 Prisma CLI 应使用的文件。支持绝对路径和相对路径。

¥The path to the desired schema.prisma file can be specified with the prisma.schema entry in the package.json file. The path defines the file the Prisma CLI should use when you run any of the CLI commands. Both absolute and relative paths are supported.

"package.json"
{
"name": "my-project",
"version": "1.0.0",
"prisma": {
"schema": "./custom-path-to-schema/schema.prisma"
}
}

从 2.7.0 及更高版本开始可以使用此功能。

¥This is available from version 2.7.0 and later.

seed

用于填充数据源的命令在 package.json 文件的 prisma.seed 条目中指定。当调用或触发 prisma db seed 时使用。

¥The command used to populate the datasource is specified in the prisma.seed entry in the package.json file. It is used when prisma db seed is invoked or triggered.

为你的数据库播种

¥See Seeding your database

"package.json"
{
"name": "my-project",
"version": "1.0.0",
"prisma": {
"seed": "node ./prisma/seed.js"
}
}

从版本 3.0.1 及更高版本开始可以使用此功能。

¥This is available from version 3.0.1 and later.

对 CLI 使用 HTTP 代理

¥Using a HTTP proxy for the CLI

Prisma CLI 支持 自定义 HTTP 代理。当位于公司防火墙后面时,这一点尤其重要。

¥Prisma CLI supports custom HTTP proxies. This is particularly relevant when being behind a corporate firewall.

要激活代理的使用,请提供以下环境变量之一:

¥To activate usage of the proxy, provide either of the following environment variables:

  • HTTP_PROXYhttp_proxy:http 流量的代理 URL,例如 http://localhost:8080

    ¥HTTP_PROXY or http_proxy: Proxy URL for http traffic, for example http://localhost:8080

  • HTTPS_PROXYhttps_proxy:https 流量的代理 URL,例如 https://localhost:8080

    ¥HTTPS_PROXY or https_proxy: Proxy URL for https traffic, for example https://localhost:8080