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
@@ -34,7 +34,7 @@ Many aspects of using Prisma ORM with D1 are just like using Prisma ORM with any
34
34
35
35
There are a number of differences between D1 and SQLite to consider. You should be aware of the following when deciding to use D1 and Prisma ORM:
36
36
37
-
-**Making schema changes**. As of [v6.6.0](https://pris.ly/release/6.6.0) and with a `prisma.config.ts` file, you can use `prisma db push`. However, if you prefer a Cloudflare first approach, you can use D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/)and the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for your migration workflows. See the [Schema migrations with Prisma ORM on D1](#schema-migrations-with-prisma-orm-on-d1) section below for more information.
37
+
-**Making schema changes**. With Prisma ORM 7, you need to use D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/)via the Wrangler CLI combined with the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for your migration workflows. See the [Schema migrations with Prisma ORM on D1](#schema-migrations-with-prisma-orm-on-d1) section below for more information.
38
38
-**Local and remote D1 (SQLite) databases**. Cloudflare provides local and remote versions of D1. The [local](https://developers.cloudflare.com/d1/build-with-d1/local-development/) version is managed using the `--local` option of the `wrangler d1` CLI and is located in `.wrangler/state`. The [remote](https://developers.cloudflare.com/d1/build-with-d1/remote-development/) version is managed by Cloudflare and is accessed via HTTP.
39
39
40
40
## How to connect to D1 in Cloudflare Workers or Cloudflare Pages
@@ -45,106 +45,13 @@ If you want to deploy a Cloudflare Worker with D1 and Prisma ORM, follow these [
45
45
46
46
## Schema migrations with Prisma ORM on D1
47
47
48
-
You can use two approaches for migrating your database schema with Prisma ORM and D1:
49
-
- Using `prisma db push` via a driver adapter in `prisma.config.ts`
50
-
- Using the Wrangler CLI
48
+
With Prisma ORM 7, the recommended approach for managing database schema migrations with Cloudflare D1 is to use the Wrangler CLI combined with `prisma migrate diff`.
51
49
52
-
### Using Prisma Migrate via a driver adapter in Prisma Config
50
+
### Using the Wrangler CLI with `prisma migrate diff`
53
51
54
-
As of [Prisma v6.6.0](https://github.com/prisma/prisma/releases/tag/6.6.0) we added support for the Prisma Config file. You can use the Prisma Config file to perform `prisma db push` and make changes to your database schema. You can learn more about [the Prisma Config file in our reference page](/orm/reference/prisma-config-reference).
52
+
Cloudflare D1 comes with its own [migration system](https://developers.cloudflare.com/d1/reference/migrations/) that works via the `wrangler d1` CLI commands.
55
53
56
-
#### 1. Install the Prisma D1 driver adapter
57
-
58
-
Run this command in your terminal:
59
-
60
-
```terminal
61
-
npm install @prisma/adapter-d1
62
-
```
63
-
64
-
#### 2. Set environment variables
65
-
66
-
In order to set up the D1 adapter, you'll need to add a few secrets to a `.env` file:
67
-
68
-
-`CLOUDFLARE_ACCOUNT_ID`: Your Cloudflare account ID, fetched via `npx wrangler whoami`.
69
-
-`CLOUDFLARE_DATABASE_ID`: Retrieved during D1 database creation. If you have an existing D1 database, you can use `npx wrangler d1 list` and `npx wrangler d1 info <database_name>` to get the ID.
70
-
-`CLOUDFLARE_D1_TOKEN`: This API token is used by Prisma ORM to communicate with your D1 instance directly. To create it, follow these steps:
4. Fill out the template: Make sure you use a recognizable name and add the **Account / D1 / Edit** permission.
75
-
5. Click **Continue to summary** and then **Create Token**.
76
-
77
-
You can then add these to your `.env` file or use them directly if they are stored in a different secret store:
78
-
79
-
```bash file=.env
80
-
CLOUDFLARE_ACCOUNT_ID="0773..."
81
-
CLOUDFLARE_DATABASE_ID="01f30366-..."
82
-
CLOUDFLARE_D1_TOKEN="F8Cg..."
83
-
```
84
-
85
-
#### 3. Set up Prisma Config file
86
-
87
-
Make sure that you have a [`prisma.config.ts`](/orm/reference/prisma-config-reference) file for your project. Then, set up the [migration driver adapter](/orm/reference/prisma-config-reference#adapter-removed) to reference D1:
As of [Prisma ORM v6.11.0](https://github.com/prisma/prisma/releases/tag/6.11.0), the D1 adapter has been renamed from `PrismaD1HTTP` to `PrismaD1`.
118
-
119
-
:::
120
-
121
-
#### 4. Migrate your database
122
-
123
-
Prisma Migrate now will run migrations against your remote D1 database based on the configuration provided in `prisma.config.ts`.
124
-
125
-
To update the remote schema with this workflow, run the following command:
126
-
127
-
```terminal
128
-
npx prisma db push
129
-
```
130
-
131
-
:::note
132
-
133
-
Note that for querying the database, you keep using the `PrismaD1` driver adapter from the `@prisma/adapter-d1` package:
134
-
135
-
```ts
136
-
import { PrismaD1 } from'@prisma/adapter-d1'
137
-
```
138
-
139
-
:::
140
-
141
-
### Using the Wrangler CLI
142
-
143
-
Cloudflare D1 comes with its own [migration system](https://developers.cloudflare.com/d1/reference/migrations/). While we recommend that you use the [native Prisma Migrate workflow](#using-prisma-migrate-via-a-driver-adapter-in-prisma-config), this migration system via the `wrangler d1 migrations` command is available.
144
-
145
-
This command doesn't help you in figuring out the SQL statements for creating your database schema that need to be put _inside_ of these migration files though. If you want to query your database using Prisma Client, it's important that your database schema maps to your Prisma schema, this is why it's recommended to generate the SQL statements from your Prisma schema.
146
-
147
-
When using D1, you can use the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for that purpose.
54
+
The Wrangler CLI provides the structure for migrations, but you need to generate the SQL statements from your Prisma schema. This is where [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) comes in - it generates SQL statements based on your Prisma schema that you can then apply using Wrangler.
148
55
149
56
#### Creating an initial migration
150
57
@@ -190,15 +97,15 @@ For the initial migration, you can use the special `--from-empty` option though:
190
97
```terminal
191
98
npx prisma migrate diff \
192
99
--from-empty \
193
-
--to-schema-datamodel ./prisma/schema.prisma \
100
+
--to-schema ./prisma/schema.prisma \
194
101
--script \
195
102
--output migrations/0001_create_user_table.sql
196
103
```
197
104
198
105
The command above uses the following options:
199
106
200
107
-`--from-empty`: The source for the SQL statement is an empty schema.
201
-
-`--to-schema-datamodel ./prisma/schema.prisma`: The target for the SQL statement is the data model in `./prisma/schema.prisma`.
108
+
-`--to-schema ./prisma/schema.prisma`: The target for the SQL statement is the schema in `./prisma/schema.prisma`.
202
109
-`--script`: Output the result as SQL. If you omit this option, the "migration steps" will be generated in plain English.
203
110
-`--output migrations/0001_create_user_table.sql`: Store the result in `migrations/0001_create_user_table.sql`.
204
111
@@ -281,15 +188,15 @@ As explained above, you now need to use `--from-local-d1` instead of `--from-emp
281
188
```terminal
282
189
npx prisma migrate diff \
283
190
--from-local-d1 \
284
-
--to-schema-datamodel ./prisma/schema.prisma \
191
+
--to-schema ./prisma/schema.prisma \
285
192
--script \
286
193
--output migrations/0002_create_post_table.sql
287
194
```
288
195
289
196
The command above uses the following options:
290
197
291
198
-`--from-local-d1`: The source for the SQL statement is the local D1 database file.
292
-
-`--to-schema-datamodel ./prisma/schema.prisma`: The target for the SQL statement is the data model in `./prisma/schema.prisma`.
199
+
-`--to-schema ./prisma/schema.prisma`: The target for the SQL statement is the schema in `./prisma/schema.prisma`.
293
200
-`--script`: Output the result as SQL. If you omit this option, the "migration steps" will be generated in plain English.
294
201
-`--output migrations/0002_create_post_table.sql`: Store the result in `migrations/0002_create_post_table.sql`.
Cloudflare D1 currently does not support transactions (see the [open feature request](https://github.com/cloudflare/workers-sdk/issues/2733)). As a result, Prisma ORM does not support transactions for Cloudflare D1. When using Prisma's D1 adapter, implicit & explicit transactions will be ignored and run as individual queries, which breaks the guarantees of the ACID properties of transactions.
329
-
330
-
### Prisma Migrate only supports remote D1 databases
331
-
332
-
The Wrangler CLI can distinguish between local and remote D1 (i.e. SQLite) database instances via the `--local` and `--remote` options. This distinction is currently not available with the [native Prisma Migrate workflow](#using-prisma-migrate-via-a-driver-adapter-in-prisma-config).
235
+
Cloudflare D1 currently does not support transactions (see the [open feature request](https://github.com/cloudflare/workers-sdk/issues/2733)). As a result, Prisma ORM does not support transactions for Cloudflare D1. When using Prisma's D1 adapter, implicit & explicit transactions will be ignored and run as individual queries, which breaks the guarantees of the ACID properties of transactions.
0 commit comments