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:

prisma/
├── 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 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 --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 属性(适用于 Prisma ORM v7):

    ¥set the schema property in prisma.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 generateprisma 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.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" 模式文件:虽然你现在可以拥有任意数量的模式文件,但你仍然需要一个地方来定义你的 generator 代码块。我们建议使用一个单独的 schema 文件,该文件显然是 "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 your generator block. 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, 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.