Schema location
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.
If you are using the
prismaSchemaFolder
preview feature any files in theprisma/schema
directory are detected automatically.
Prisma Schema location
The Prisma CLI looks for the Prisma Schema in the following locations, in the following order:
-
The location specified by the
--schema
flag, which is available when youintrospect
,generate
,migrate
, andstudio
:prisma generate --schema=./alternative/schema.prisma
-
The location specified in the
package.json
file (version 2.7.0 and later):"prisma": {
"schema": "db/schema.prisma"
} -
Default locations:
./prisma/schema.prisma
./schema.prisma
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.
Multi-file Prisma Schema (Preview)
If you prefer splitting your Prisma schema into multiple files, you can have a setup that looks as follows:
my-app/
├─ ...
├─ prisma/
│ ├─ schema/
│ │ ├─ post.prisma
│ │ ├─ schema.prisma
│ │ ├─ user.prisma
├─ ...
Usage
You can split your Prisma schema into multiple files by enabling the prismaSchemaFolder
Preview feature on your generator
block:
generator client {
provider = "prisma-client-js"
previewFeatures = ["prismaSchemaFolder"]
}
As of v6.6.0, you must always explicitly specify the location of your Prisma schema folder. There is no "magic" detection of the Prisma schema folder in a default location any more.
You can do this in either of three ways:
- pass the the
--schema
option to your Prisma CLI command (e.g.prisma migrate dev --schema ./prisma/schema
) - set the
prisma.schema
field inpackage.json
:// package.json
{
"prisma": {
"schema": "./schema"
}
} - set the
schema
property inprisma.config.ts
:import path from 'node:path'
import type { PrismaConfig } from 'prisma'
export default {
earlyAccess: true,
schema: path.join('prisma', 'schema'),
} satisfies PrismaConfig<Env>
You also must place the migrations
directory next to the .prisma
file that defines the datasource
block.
For example, assuming schema.prisma
defines the datasource
, here's how how need to place the migrations folder:
# `migrations` and `schema.prisma` are on the same level
.
├── migrations
├── models
│ ├── posts.prisma
│ └── users.prisma
└── schema.prisma
How to use existing Prisma CLI commands with multiple Prisma schema files
For most Prisma CLI commands, no changes will be necessary to work with a multi-file Prisma schema. Only in the specific cases where you need to supply a schema via an option will a command need to be changed. In these cases, simply replace references to a file with a directory. As an example, the following prisma db push
command:
npx prisma db push --schema custom/path/to/my/schema.prisma
becomes the following:
npx prisma db push --schema custom/path/to/my/schema # note this is now a directory!
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:
-
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 inpost.prisma
. Try to avoid having “kitchen sink” schema files. -
Use clear naming conventions: schema files should be named clearly and succinctly. Use names like
user.prisma
andpost.prisma
and notmyModels.prisma
orCommentFeaturesSchema.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
andgenerator
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
, andbase.prisma
are a few we’ve seen that work well.
Examples
Our fork of dub
by dub.co is a great example of a real world project adapted to use a multi-file Prisma Schema.
Learn more about the prismaSchemaFolder
preview feature
To give feedback on the prismaSchemaFolder
Preview feature, please refer to our dedicated Github discussion.