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:
.
├── migrations
├── models
│ ├── posts.prisma
│ ├── users.prisma
│ └── ... other `.prisma` files
└── schema.prisma
用法
¥Usage
使用多文件 Prisma 模式时,必须始终明确指定包含 .prisma 文件和 datasource 块的目录位置。
¥When using a multi-file Prisma schema, you must always explicitly specify the location of the directory that contains the .prisma file with your datasource 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属性:¥set the
schemaproperty inprisma.config.ts:import path from 'node:path'
import type { PrismaConfig } from 'prisma'
export default {
schema: path.join('prisma'),
} satisfies PrismaConfig注意我们建议使用 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.
以上所有示例均假设你的 datasource 块在 prisma 目录内的 .prisma 文件中定义。
¥All examples above assume that your datasource block is defined in a .prisma file inside the prisma directory.
你还必须将 migrations 目录放在定义 datasource 块的 .prisma 文件旁边。
¥You also must place the migrations directory next to the .prisma file that defines the datasource block.
例如,假设 schema.prisma 定义了 datasource,则迁移文件夹的放置方式如下:
¥For example, assuming schema.prisma defines the datasource, here's how how need to place the migrations folder:
# `migrations` and `schema.prisma` must be on the same level
.
├── migrations
├── models
│ ├── posts.prisma
│ └── users.prisma
└── schema.prisma
多文件 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" 模式文件:虽然你现在可以拥有任意数量的模式文件,但你仍然需要一个定义
datasource和generator块的地方。我们建议使用一个显然是 "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
datasourceandgeneratorblocks. We recommend having a single schema file that's obviously the "main" file so that these blocks are 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.