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
{{ message }}
Repository navigation
Commit 2de4da9
Browse filesBrowse the repository at this point in the historyBrowse files
`@prisma/orm-postgres` is the library your code imports, and `prisma` is the command-line tool. `dotenv` loads `.env`, `tsx` runs `index.ts` without a build step, and `typescript` 5.9 or newer is a peer dependency of `@prisma/orm-postgres`.
46
+
`@prisma/orm-postgres` is the library your code imports, and `prisma` is the command-line tool. `pg` is the PostgreSQL driver, a peer dependency of `@prisma/orm-postgres`, and `@types/pg` gives TypeScript its types. `dotenv` loads `.env`, `tsx` runs `index.ts` without a build step, and `typescript` 5.9 or newer is a peer dependency of `@prisma/orm-postgres`.
47
47
48
48
`temporal-polyfill` is a peer dependency of `@prisma/orm-postgres` too. It adds `Temporal`, the newer JavaScript date and time API, to runtimes such as Node.js 24 that do not have it built in. npm installs it for you, but with Yarn you add it yourself.
49
49
50
-
The two Prisma packages have different version numbers, for example `prisma` 8.0.0-rc.34 and `@prisma/orm-postgres` 8.0.0-rc.24. That is normal, and any two `latest` versions work together.
50
+
The two Prisma packages have different version numbers, for example `prisma` 8.0.0-rc.34 and `@prisma/orm-postgres` 8.0.0-rc.25. That is normal, and any two `latest` versions work together.
Set `newline` when anyone works on the project from Windows; `"LF"` is the usual choice. Without it, on Windows `contract format` writes `CRLF` but the [Prisma editor extension](/orm/contract-authoring/editor-support) writes `LF`. Each one rewrites every line the other wrote, so Git shows the whole file as changed. The editor extension reads `formatter` from `prisma.config.ts` too, so once `newline` is set, the command and the editor write the same line endings on every machine.
280
280
281
-
`formatter` needs `@prisma/orm-postgres`or`@prisma/orm-mongo`at `8.0.0-rc.24` or later. An earlier release ignores the setting without an error, and `contract format` still indents with two spaces. `npm ls @prisma/orm-postgres`, or the same command with `@prisma/orm-mongo`, prints the release you have.
281
+
`@prisma/orm-postgres`and`@prisma/orm-mongo`read `formatter` since `8.0.0-rc.24`. An earlier release ignores the setting without an error, and `contract format` still indents with two spaces. `npm ls @prisma/orm-postgres`, or the same command with `@prisma/orm-mongo`, prints the release you have.
282
282
283
283
If `indent` or `newline` has a value the table does not allow, every command that reads the config fails with `CLI.CONFIG_FIELD_INVALID`. The message names the field, as in `formatter.indent must be at least 1 (was 0)`.
Copy file name to clipboardExpand all lines: apps/docs/content/docs/cli/contract-infer.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,7 @@ In a list column's type, a `?` means something different depending on where it i
58
58
59
59
A list column that Prisma ORM migrations created cannot hold `NULL` elements, because the migration adds a check that stops it, so it gets no `?` before the brackets. Any other list column gets one.
60
60
61
-
Each column gets a type such as `String(storage: varchar, length: 20)`, `Int(storage: int2)`, `Bit(8)`, `Varbit(16)`, `Xml`, `Money`, or `Duration(precision: 3)`, written with the fewest parameters that describe the column exactly. Where a [convenience type](/orm/contract-authoring/psl-syntax#models-and-fields) gives the column's type, the contract writes it with only the arguments that differ from its defaults. The types come from Prisma ORM's types for your database and from the extension packs listed under `extensions` in `prisma.config.ts`. An extension pack is the Prisma ORM package for a database extension, such as pgvector.
61
+
Each column gets a type such as `String(storage: varchar, length: 20)`, `Int(storage: int2)`, `Json(storage: jsonb)`, `Bit(8)`, `Varbit(16)`, `Xml`, `Money`, or `Duration(precision: 3)`, written with the fewest parameters that describe the column exactly. Where a [convenience type](/orm/contract-authoring/psl-syntax#models-and-fields) gives the column's type, the contract writes it with only the arguments that differ from its defaults. The types come from Prisma ORM's types for your database and from the extension packs listed under `extensions` in `prisma.config.ts`. An extension pack is the Prisma ORM package for a database extension, such as pgvector.
62
62
63
63
When a column has a type that none of these provide, the command writes no file. It fails with `CONTRACT.INFER_COLUMN_TYPE_UNCLAIMED` and lists every such column, such as `public.doc.meta: hstore`. To get past the error, do one of the following for each listed column:
metaDescription: Learn how to apply pending Prisma ORM on-disk migrations with the db migrate command.
7
7
---
8
8
9
-
`db migrate` applies the migrations on disk that the database has not run yet, including the ones that extension packages ship. It runs the extensions' migrations first, in alphabetical order of extension, and then yours. It never writes a new migration; [`migration plan`](/cli/migration-plan) does that.
9
+
`db migrate` applies the migrations on disk that the database has not run yet, including the ones that extension packages ship. It runs the extensions' migrations first, in alphabetical order of extension, and then yours. It never writes a new migration; [`migration plan`](/cli/migration-plan) does that. It obeys the database's [policies](/orm/migrations/database-policies): a database whose change policy does not include `migrate` refuses it, and one whose data protection policy forbids an operation in a pending migration refuses the whole run before it applies anything.
10
10
11
11
Prisma ORM keeps one migration history for your own models, called the `app` space, in `migrations/app/`, and one for each [extension package](/orm/extensions/using-extensions) that ships its own migrations, such as pgvector. Each of these histories is a [contract space](/orm/migrations/the-migration-graph#terms-used-on-this-page).
12
12
@@ -26,6 +26,7 @@ npx prisma db migrate --db "$DATABASE_URL"
26
26
|`--to <contract>`| Applies migrations up to a target contract state. Accepts a [contract reference](/cli/migration-ref#contract-reference-forms): a hash, a hash prefix, a [ref name](#contract-references), a migration directory name, [`<dir>^`](#contract-references), [`@contract`](#contract-references), [`@db`](#contract-references), or [`@empty`](#contract-references). |
27
27
|`--advance-ref <name>`| After a successful run, points the named [ref](/cli/migration-ref) at the contract state the database now matches. Pass `--advance-ref db` in development. |
28
28
|`--show`| Prints the migrations `db migrate` would run, and runs nothing. |
29
+
|`--permit-policy-change`| With `--to <ref>`, checks the run against the ref's declared [policies](/orm/migrations/database-policies) and writes them into the database when it succeeds. Without it, a ref whose policies differ from the database's fails with `MIGRATION.POLICY_MISMATCH`. |
29
30
|`--from <contract>`| Use it with `--show`, where it sets the starting state for the preview. Accepts a [contract reference](/cli/migration-ref#contract-reference-forms): a hash, a hash prefix, a [ref name](#contract-references), a migration directory name, [`<dir>^`](#contract-references), [`@contract`](#contract-references), [`@db`](#contract-references), or [`@empty`](#contract-references). |
30
31
|`--config <path>`| Read this config file instead of `./prisma.config.ts`. |
Copy file name to clipboardExpand all lines: apps/docs/content/docs/cli/db-update.mdx
+16-3Lines changed: 16 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,8 @@ metaDescription: Learn how to reconcile a database with the current Prisma ORM c
10
10
11
11
Like Prisma ORM 7's `db push`, use it when you want a database to match your contract and do not need a migration file for the change, for example on your development database while you try out a contract change.
12
12
13
+
A database whose [change policy](/orm/migrations/database-policies) does not include `reconcile` refuses `db update` with `MIGRATION.CHANGE_POLICY_FORBIDS`. Its [data protection policy](/orm/migrations/database-policies#data-protection-policy) decides whether the update asks before it loses data or changes access, refuses, or goes ahead without asking.
14
+
13
15
## Usage
14
16
15
17
```npm
@@ -27,6 +29,7 @@ npx prisma db update --db "$DATABASE_URL"
27
29
|`--delete <subject>`| Lets the update lose the data of a model, a field, a table no model stores, every model of a namespace the contract removed, or, on PostgreSQL and SQLite, an enum value the contract removed, a field of a value object, or the rows of a variant. Repeatable. |
28
30
|`--convert <Variant>`| On PostgreSQL and SQLite, rewrites the stored discriminator value of a variant whose value changed. Repeatable. |
29
31
|`--allow <Model>`| Lets one operation change who can read or write the model's data. Repeatable. |
32
+
|`--permit-policy-change`| With `--to <ref>`, checks the run against the ref's declared [policies](/orm/migrations/database-policies) and writes them into the database when it succeeds. Without it, a ref whose policies differ from the database's fails with `MIGRATION.POLICY_MISMATCH`. |
30
33
|`--advance-ref <name>`| After the update, points the named [ref](/cli/migration-ref) at the contract state the database now matches. Without it, `db update` points the `db` ref there when you leave out `--db`, and changes no ref when you pass `--db`. |
31
34
|`--config <path>`| Read this config file instead of `./prisma.config.ts`. |
32
35
|`--json`| Prints a machine-readable result. |
@@ -60,6 +63,16 @@ In a terminal, the command asks the questions you did not answer with a flag. In
60
63
61
64
Dropping an index, a unique or foreign-key constraint, a check, a default or a native enum type loses no data, so the update asks nothing about it.
62
65
66
+
### A changed stored name [#a-changed-stored-name]
67
+
68
+
On PostgreSQL and SQLite, a model, field or enum member that keeps its name in the contract is the same object, even when what it is stored as changes. When only its `@@map`, its `@map`, or the value an enum member stores changes, the update renames the table or column, with the constraints and indexes named after it, or replaces the stored value in the rows, and asks nothing. A table or column is renamed only when its control policy is `managed` in both contracts. A `--delete` for such a change fails with `CLI.CONSENT_UNUSED`, because nothing asks about it.
69
+
70
+
To start with an empty table under the new name instead, make two changes, each applied before the next: remove the model and answer `--delete`, then add it back with its new `@@map`.
71
+
72
+
Swaps, chains and takeovers are refused with `MIGRATION.PLANNING_FAILED`: two models or fields that trade tables or columns, one that moves to a name another frees, one that takes the storage of one the change removes, and enum members that trade stored values. Make the change in two steps through a name nothing uses, such as `prisma_swap_users`, applied one after the other.
73
+
74
+
MySQL and MongoDB plan these changes as before. Before `8.0.0-rc.25`, a changed `@@map` was planned as dropping the table and creating an empty one, and asked for `--delete`.
75
+
63
76
`db update` names each subject by model and field from the contract the database was last updated to, which it stores when it advances a ref. With `--db`, pass `--advance-ref <name>` on every run so that contract is stored for the next run. Without a stored contract, each subject is its storage name, and a run with `--rename` fails with `MIGRATION.STATEMENT_ORIGIN_UNKNOWN`. Adding `--advance-ref` to that run does not help, because the missing contract is the one the database is at before it. To store it, emit the contract the database is at, run `db update --advance-ref db --dry-run` with the same `--db`, and check that it plans no operations. Then run it again without `--dry-run`, which changes nothing in the database and stores the contract. Then emit the new contract and run the rename.
64
77
65
78
On PostgreSQL, a namespace is renamed, moved or removed with the same flags. `--rename billing:finance` renames the schema, and everything in it stays in it. `--rename public.Invoice:billing.Invoice` moves the model's table to another schema with its rows, constraints, indexes and policies, and creates the schema first when the database does not have it. Namespace renames run before the other renames, whatever order you give them in. `public` and a schema another contract space declares are not renamed.
@@ -74,12 +87,12 @@ The schema can also hold objects that no contract declares. A table created by h
74
87
75
88
The command leaves alone a schema that no contract declared, such as Supabase's `auth`, and a namespace that only another contract space declares. Before `8.0.0-rc.20`, the command did not ask about a removed namespace and left its tables in place.
76
89
77
-
On PostgreSQL and SQLite, removing a member from an `enum` block, or changing the value a member stores, is asked about too, because rows may hold the old value. The update asks once for each value, however many columns hold it:
90
+
On PostgreSQL and SQLite, removing a member from an `enum` blockis asked about too, because rows may hold its value. The update asks once for each value, however many columns hold it:
78
91
79
92
- If you renamed the member, answer with `--rename Status.ARCHIVED:Status.RETIRED`. The update changes the stored value in every column and list that holds it.
80
93
- To let the value go, answer with `--delete Status.ARCHIVED`. Optional columns that hold it are set to `NULL`, and lists lose it. A required column cannot take `NULL`, so while any of its rows hold the value, the apply fails with `MIGRATION.PRECHECK_FAILED` before it changes anything. The error counts those rows and says how to keep them.
81
94
82
-
A member you rename while keeping its stored value, such as `RETIRED = "archived"` in place of `ARCHIVED = "archived"`, changes no rows, so nothing is asked. A member that keeps its name but stores a new value, and two members that swap their stored values, can only be answered with `--delete`. `db update` cannot keep those rows in one run. Its question says to plan the change with [`migration plan --from <hash>`](/cli/migration-plan#renames-and-data-loss) instead, or, for a member that keeps its name, to rename it to a new member and back in two runs of `db update`. For a swap, only the migration works.
95
+
A member you rename while keeping its stored value, such as `RETIRED = "archived"` in place of `ARCHIVED = "archived"`, changes no rows, so nothing is asked. A member that keeps its name but stores a new value is [a changed stored name](#a-changed-stored-name): the update replaces the value in the rows and asks nothing. Two members that swap their stored values are refused; make the swap in two runs through a value nothing uses.
83
96
84
97
Native PostgreSQL enums (`pg.enum(...)`) are not affected. Before `8.0.0-rc.21`, PostgreSQL failed at apply when a row held the removed value, and SQLite planned nothing.
85
98
@@ -96,7 +109,7 @@ A [variant](/orm/contract-authoring/psl-syntax#base-models-and-variants) whose d
96
109
-`--rename Bug:Defect` of a variant whose value also changed rewrites the value in the same way.
97
110
-`--delete Bug` deletes the rows that hold the old value, then drops a removed variant's own table or the columns only it used.
98
111
99
-
On MongoDB, `--rename` of a value object field or of a variant whose discriminator value changes is refused, and so is `--convert` of a variant.
112
+
On MongoDB, changes inside documents are asked about too, since `8.0.0-rc.25`: a field a value object or an embedded model loses, a stored value an enum loses, and a variant that is removed or whose discriminator value changes. They take the same answers as in [`migration plan`](/cli/migration-plan#renames-and-data-loss). With `--no-interactive` and no answers, the update fails with `CLI.CONSENT_REQUIRED` and lists the flags to add.
100
113
101
114
On MongoDB, a field type change that can lose values is asked about, as on SQL. Answer with `--delete` and the field, as in `--delete User.age`, which keeps the field and casts its stored values with `$convert`. To write the conversion yourself, use `migration plan --convert` instead, because `db update` writes no migration for you to edit.
0 commit comments