You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/200-orm/100-prisma-schema/10-overview/04-location.mdx
+34-16Lines changed: 34 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,7 @@ Run prisma generate to generate Prisma Client.
48
48
If you prefer splitting your Prisma schema into multiple files, you can have a setup that looks as follows:
49
49
50
50
```
51
-
.
51
+
prisma/
52
52
βββ migrations
53
53
βββ models
54
54
β βββ posts.prisma
@@ -65,7 +65,7 @@ Multi-file Prisma schemas are Generally Available since [v6.7.0](https://pris.ly
65
65
66
66
### Usage
67
67
68
-
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.
68
+
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).
69
69
70
70
You can do this in either of three ways:
71
71
@@ -79,14 +79,21 @@ You can do this in either of three ways:
79
79
}
80
80
}
81
81
```
82
-
- set the `schema` property in [`prisma.config.ts`](/orm/reference/prisma-config-reference#schema):
82
+
- set the `schema` property in [`prisma.config.ts`](/orm/reference/prisma-config-reference#schema) ( for Prisma ORM v7):
83
83
```ts
84
-
importpathfrom'node:path'
85
-
importtype { PrismaConfig } from'prisma'
86
-
87
-
exportdefault {
88
-
schema: path.join('prisma'),
89
-
} satisfiesPrismaConfig
84
+
import { defineConfig, env } from'prisma/config'
85
+
import'dotenv/config'
86
+
87
+
exportdefaultdefineConfig({
88
+
schema: 'prisma/schema.prisma',
89
+
migrations: {
90
+
path: 'prisma/migrations',
91
+
seed: 'tsx prisma/seed.ts',
92
+
},
93
+
datasource: {
94
+
url: env('DATABASE_URL'),
95
+
},
96
+
})
90
97
```
91
98
92
99
:::note
@@ -95,29 +102,40 @@ You can do this in either of three ways:
95
102
96
103
:::
97
104
98
-
All examples above assume that your `datasource` block is defined in a `.prisma` file inside the `prisma` directory.
105
+
:::warning
99
106
100
-
You also must place the `migrations` directory next to the `.prisma` file that defines the `datasource` block.
107
+
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`.
101
108
102
-
For example, assuming `schema.prisma` defines the `datasource`, here's how how need to place the migrations folder:
109
+
:::
110
+
111
+
You also must place the `migrations` directory at the same level as your `schema.prisma` file.
112
+
113
+
For example, assuming `schema.prisma` defines the `generator` block, here's the correct directory structure:
103
114
104
115
```
105
-
# `migrations` and `schema.prisma` must be on the same level
106
-
.
116
+
# All files must be inside the `prisma/` directory
117
+
# `migrations` and `schema.prisma` must be at the same level
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.
129
+
130
+
:::
131
+
114
132
### Tips for multi-file Prisma Schema
115
133
116
134
We've found that a few patterns work well with this feature and will help you get the most out of it:
117
135
118
136
- 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`.
119
137
- 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`.
120
-
- 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.
138
+
- 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.
0 commit comments