Schema 位置
Prisma Schema 的默认名称是 prisma 文件夹中的单个文件 schema.prisma。当你的模式这样命名时,Prisma CLI 将自动检测它。
¥The default name for the Prisma Schema is a single file schema.prisma in your prisma folder. When your schema is named like this, the Prisma CLI will detect it automatically.
Prisma Schema 位置
¥Prisma Schema location
Prisma CLI 按以下顺序在以下位置查找 Prisma Schema:
¥The Prisma CLI looks for the Prisma Schema in the following locations, in the following order:
-
--schema旗 指定的位置,当你introspect、generate、migrate和studio时可用:¥The location specified by the
--schemaflag, which is available when youintrospect,generate,migrate, andstudio:prisma generate --schema=./alternative/schema.prisma -
package.json文件中指定的位置(2.7.0 及更高版本):¥The location specified in the
package.jsonfile (version 2.7.0 and later):"prisma": {
"schema": "db/schema.prisma"
} -
默认位置:
¥Default locations:
-
./prisma/schema.prisma -
./schema.prisma
-
Prisma CLI 输出将要使用的模式的路径。以下示例显示了 prisma db pull 的终端输出:
¥The Prisma CLI outputs the path of the schema that will be used. The following example shows the terminal output for prisma db pull:
Environment variables loaded from .env
Prisma Schema loaded from prisma/schema.prisma
Introspecting based on datasource defined in prisma/schema.prisma …
✔ Introspected 4 models and wrote them into prisma/schema.prisma in 239ms
Run prisma generate to generate Prisma Client.
多文件 Prisma 模式
¥Multi-file Prisma schema
如果你希望将 Prisma 模式拆分为多个文件,你可以进行如下设置:
¥If you prefer splitting your Prisma schema into multiple files, you can have a setup that looks as follows:
prisma/
├── migrations
├── models
│ ├── posts.prisma
│ ├── users.prisma
│ └── ... other `.prisma` files
└── schema.prisma
用法
¥Usage
使用多文件 Prisma schema 时,必须始终显式指定包含 schema 文件(包括包含 generator 代码块的主 schema.prisma 文件)的目录位置。
¥When using a multi-file Prisma schema, you must always explicitly specify the location of the directory that contains your schema files (including the main schema.prisma file with your generator block).
你可以通过以下三种方式之一执行此操作:
¥You can do this in either of three ways:
-
将
--schema选项传递给你的 Prisma CLI 命令(例如prisma migrate dev --schema ./prisma)。¥pass the the
--schemaoption to your Prisma CLI command (e.g.prisma migrate dev --schema ./prisma) -
在
package.json中设置prisma.schema字段:¥set the
prisma.schemafield inpackage.json:// package.json
{
"prisma": {
"schema": "./prisma"
}
} -
在
prisma.config.ts中设置schema属性(适用于 Prisma ORM v7):¥set the
schemaproperty inprisma.config.ts( for Prisma ORM v7):import { defineConfig, env } from 'prisma/config'
import 'dotenv/config'
export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
seed: 'tsx prisma/seed.ts',
},
datasource: {
url: env('DATABASE_URL'),
},
})注意我们建议使用 Prisma 配置文件 指定 Prisma 模式的位置。这是与其他配置选项一起指定 Prisma 模式位置的最灵活方法。
¥We recommend using the Prisma Config file to specify the location of your Prisma schema. This is the most flexible way to specify the location of your Prisma schema alongside other configuration options.
schema.prisma 文件(包含你的 generator 代码块)必须位于你在架构配置中指定的同一目录中。例如,如果你配置了 schema: 'prisma',则 schema.prisma 文件必须位于 prisma/schema.prisma 目录下,而不能位于子目录(例如 prisma/models/schema.prisma)下。
¥The schema.prisma file (which contains your generator block) must be located in the same directory that you specify in your schema configuration. For example, if you configure schema: 'prisma', your schema.prisma file must be at prisma/schema.prisma, not in a subdirectory like prisma/models/schema.prisma.
你还必须将 migrations 目录与 schema.prisma 文件放在同一层级。
¥You also must place the migrations directory at the same level as your schema.prisma file.
例如,假设 schema.prisma 定义了 generator 代码块,以下是正确的目录结构:
¥For example, assuming schema.prisma defines the generator block, here's the correct directory structure:
# All files must be inside the `prisma/` directory
# `migrations` and `schema.prisma` must be at the same level
prisma/
├── migrations
├── models
│ ├── posts.prisma
│ └── users.prisma
└── schema.prisma # Contains generator block
如果你的模式文件位于 prisma/ 目录(如上所示),则 Prisma CLI 命令(例如 prisma generate 和 prisma migrate dev)无需额外配置即可正常工作,因为 ./prisma/schema.prisma 是默认位置。
¥If your schema files are in a prisma/ directory (as shown above), the Prisma CLI commands like prisma generate and prisma migrate dev will work without additional configuration, as ./prisma/schema.prisma is a default location.
多文件 Prisma Schema 提示
¥Tips for multi-file Prisma Schema
我们发现一些模式可以很好地与此功能配合使用,并将帮助你充分利用它:
¥We've found that a few patterns work well with this feature and will help you get the most out of it:
-
按域组织你的文件:将相关模型分组到同一个文件中。例如,将所有与用户相关的模型保留在
user.prisma中,而将与帖子相关的模型保留在post.prisma中。¥Organize your files by domain: group related models into the same file. For example, keep all user-related models in
user.prismawhile post-related models go inpost.prisma. -
使用清晰的命名约定:模式文件应清晰简洁地命名。使用
user.prisma和post.prisma等名称,而不是myModels.prisma或CommentFeaturesSchema.prisma。¥Use clear naming conventions: schema files should be named clearly and succinctly. Use names like
user.prismaandpost.prismaand notmyModels.prismaorCommentFeaturesSchema.prisma. -
拥有一个明显的 "main" 模式文件:虽然你现在可以拥有任意数量的模式文件,但你仍然需要一个地方来定义你的
generator代码块。我们建议使用一个单独的 schema 文件,该文件显然是 "main" 文件,以便轻松找到此代码块。main.prisma、schema.prisma和base.prisma是我们见过的一些运行良好的功能。¥Have an obvious "main" schema file: while you can now have as many schema files as you want, you'll still need a place where you define your
generatorblock. We recommend having a single schema file that's obviously the "main" file so that this block is easy to find.main.prisma,schema.prisma, andbase.prismaare a few we've seen that work well.
示例
¥Examples
我们的 dub.co 的 dub 分支是一个很好的例子,它是一个适合使用多文件 Prisma Schema 的真实世界项目。
¥Our fork of dub by dub.co is a great example of a real world project adapted to use a multi-file Prisma Schema.