Skip to main content

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:

  1. --schema 指定的位置,当你 introspectgeneratemigratestudio 时可用:

    ¥The location specified by the --schema flag, which is available when you introspect, generate, migrate, and studio:

    prisma generate --schema=./alternative/schema.prisma
  2. package.json 文件中指定的位置(2.7.0 及更高版本):

    ¥The location specified in the package.json file (version 2.7.0 and later):

    "prisma": {
    "schema": "db/schema.prisma"
    }
  3. 默认位置:

    ¥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
注意

v6.7.0 起,多文件 Prisma 架构已正式发布。在此之前,可以通过 prismaSchemaFolder 预览功能标志使用它们。

¥Multi-file Prisma schemas are Generally Available since v6.7.0. Before that, they could be used via the prismaSchemaFolder Preview feature flag.

用法

¥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 --schema option to your Prisma CLI command (e.g. prisma migrate dev --schema ./prisma)

  • package.json 中设置 prisma.schema 字段:

    ¥set the prisma.schema field in package.json:

    // package.json
    {
    "prisma": {
    "schema": "./prisma"
    }
    }
  • prisma.config.ts 中设置 schema 属性:

    ¥set the schema property in prisma.config.ts:

    import path from 'node:path'
    import type { PrismaConfig } from 'prisma'

    export default {
    earlyAccess: true,
    schema: path.join('prisma'),
    } satisfies PrismaConfig<Env>

以上所有示例均假设你的 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.prisma while post-related models go in post.prisma.

  • 使用清晰的命名约定:模式文件应清晰简洁地命名。使用 user.prismapost.prisma 等名称,而不是 myModels.prismaCommentFeaturesSchema.prisma

    ¥Use clear naming conventions: schema files should be named clearly and succinctly. Use names like user.prisma and post.prisma and not myModels.prisma or CommentFeaturesSchema.prisma.

  • 拥有一个明显的 "main" 模式文件:虽然你现在可以拥有任意数量的模式文件,但你仍然需要一个定义 datasourcegenerator 块的地方。我们建议使用一个显然是 "main" 文件的模式文件,以便轻松找到这些块。main.prismaschema.prismabase.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 datasource and generator blocks. 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, and base.prisma are 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.