# db update (/docs/cli/db-update) > For the complete Prisma documentation index, see [llms.txt](https://www.prisma.io/docs/llms.txt). A markdown version of any docs page is available by appending `.md` to its URL. Update a database to match the current Prisma ORM contract. Location: CLI > db update `db update` compares the live database with the emitted contract and applies the changes that make the database match it, whether or not the database was set up with `db init`. After it applies the changes, it signs the database, as [`db sign`](https://www.prisma.io/docs/cli/db-sign) does: it records there which contract the database now matches. 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. A database whose [change policy](https://www.prisma.io/docs/orm/migrations/database-policies) does not include `reconcile` refuses `db update` with `MIGRATION.CHANGE_POLICY_FORBIDS`. Its [data protection policy](https://www.prisma.io/docs/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. A database that records no policy allows `db update`, which then asks before it loses data or changes access. ## Usage [#usage] #### npm ```bash npx prisma db update --db "$DATABASE_URL" ``` #### pnpm ```bash pnpm prisma db update --db "$DATABASE_URL" ``` #### yarn ```bash yarn prisma db update --db "$DATABASE_URL" ``` #### bun ```bash bunx prisma db update --db "$DATABASE_URL" ``` ## Options [#options] | Option | What it does | | ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `--db ` | Connects to the database. | | `--dry-run` | Shows planned operations without applying them. | | `--to ` | Updates to a specific contract state. Accepts a [contract reference](https://www.prisma.io/docs/cli/migration-ref#contract-reference-forms): a hash, a hash prefix, a [ref name](#contract-references), a migration directory name, or [`^`](#contract-references). It refuses `@contract`, `@db`, and `@empty` with the error code `MIGRATION.REF_WRONG_GRAMMAR`. To update to the contract in `contract.json`, leave out `--to`. | | `--rename :` | Renames a namespace, a model or a field and keeps its rows, instead of dropping and creating it, or moves a model to another namespace. On PostgreSQL, SQLite and MongoDB, also renames an enum value in the rows that hold it, as in `--rename Status.ARCHIVED:Status.RETIRED`, a field of a value object, as in `--rename GeoPoint.lat:GeoPoint.latitude`, and a variant. Repeatable. See [Renames and data loss](#renames-and-data-loss). | | `--delete ` | 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, SQLite and MongoDB, an enum value the contract removed, a field of a value object, or the rows or documents of a variant. Repeatable. | | `--convert ` | On PostgreSQL, SQLite and MongoDB, rewrites the stored discriminator value of a variant whose value changed. Repeatable. | | `--allow ` | Lets one operation change who can read or write the model's data. Repeatable. | | `--permit-policy-change` | With `--to `, checks the run against the ref's declared [policies](https://www.prisma.io/docs/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`. Pass it only when the owner of the database means to apply the ref's policies. | | `--advance-ref ` | After the update, points the named [ref](https://www.prisma.io/docs/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`. | | `--config ` | Read this config file instead of `./prisma.config.ts`. | | `--json` | Prints a machine-readable result. | The [global flags](https://www.prisma.io/docs/cli/global-flags), such as `--no-interactive` and `--confirm`, apply too. ### Contract references [#contract-references] A ref name is a name you gave a contract state with [`migration ref set`](https://www.prisma.io/docs/cli/migration-ref), such as `prod`. `^` is a migration directory name followed by `^`, and it names the contract state before that migration, while the directory name alone names the state after it. [Contract references each command accepts](https://www.prisma.io/docs/cli/migration-ref#contract-reference-forms) lists the forms for every command. ## What happens to the db ref [#how-the-db-ref-moves] The [ref](https://www.prisma.io/docs/cli/migration-ref) named `db` records which contract state your development database is at, which is the database `db.connection` in `prisma.config.ts` connects to. So when you run `db update` without `--db`, it uses that connection and points the `db` ref at the contract it just applied. When you pass `--db`, `db update` treats the URL as some other database and leaves the `db` ref alone, even when the URL is the same one the config holds. Pass `--advance-ref db` alongside `--db` to update the ref anyway. The `db` ref is what [`migration plan`](https://www.prisma.io/docs/cli/migration-plan) uses as its starting point when you do not pass `--from`. Passing `--db` only leaves the ref unchanged; it never removes a `db` ref that already exists. So if you always pass `--db` without `--advance-ref db`, the ref keeps naming the state it was last set to, and the next `migration plan` starts from that out-of-date contract. If the ref was never created, `migration plan` plans from an empty database while `migrations/app/` is empty, and refuses with `MIGRATION.PLAN_ORIGIN_UNKNOWN` once migrations exist on disk. ## Renames and data loss [#renames-and-data-loss] Before it applies anything, `db update` asks one question for each model, field or table whose data the update would lose, such as a dropped column or a type change that can change values. Answer each one on the command line: - `--delete ` lets the data go. A subject is `Model`, `Model.field`, or, for data no model stores, its storage name, such as `public.audit_log`. - `--rename :` keeps the data. The update renames the table or column, and the constraints and indexes named after it, instead of dropping and creating it. Each side is `Model`, `namespace.Model`, `Model.field` or `namespace.Model.field`, and a field's model is named as the new contract names it: `--rename Profile:User --rename User.name:User.fullName`. It also asks before each operation that changes who can read or write a model's data, such as dropping a row-level-security policy or disabling row-level security. The question ends with "would widen who can read and write its data" or "would change who can read and write its data". Answer each with `--allow `; two such operations on one model take `--allow User --allow User`. A script reading `--dry-run --json` finds these operations in `accessChanges`, which before `8.0.0-rc.20` was named `accessWidening`. #### npm ```bash npx prisma db update --db "$DATABASE_URL" --advance-ref db --no-interactive --delete Legacy --rename User.name:User.fullName ``` #### pnpm ```bash pnpm prisma db update --db "$DATABASE_URL" --advance-ref db --no-interactive --delete Legacy --rename User.name:User.fullName ``` #### yarn ```bash yarn prisma db update --db "$DATABASE_URL" --advance-ref db --no-interactive --delete Legacy --rename User.name:User.fullName ``` #### bun ```bash bunx prisma db update --db "$DATABASE_URL" --advance-ref db --no-interactive --delete Legacy --rename User.name:User.fullName ``` In a terminal, the command asks the questions you did not answer with a flag. In a CI job or a run with `--no-interactive`, it fails with [`CLI.CONSENT_REQUIRED`](https://www.prisma.io/docs/cli/error-reference#CLI.CONSENT_REQUIRED) instead. The error lists each question still unanswered, and its next actions give the flags to add. A `--delete` or `--allow` that answers no question fails the run with `CLI.CONSENT_UNUSED`. To list the questions without applying anything, run with `--dry-run --json` and read `dataLoss` and `accessChanges`. The global `--confirm` flag does not answer these questions: only `--rename`, `--delete`, `--allow` or your answers in a terminal do. 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. ### A changed stored name [#a-changed-stored-name] On PostgreSQL and SQLite, a model, field or enum member keeps its identity as long as it keeps its name in the contract, even when the name it is stored under changes. So when you change only a model's `@@map`, a field's `@map`, or the value an enum member stores, the update keeps the data and asks nothing: ```prisma title="contract.prisma" model User { id Int @id email String @map("email_address") // was @map("email") @@map("accounts") // was @@map("users") } ``` The update renames the table `users` to `accounts` and the column `email` to `email_address`, together with the constraints and indexes named after them. For an enum member that keeps its name but stores a new value, the update replaces the old value in the rows. A table or column is renamed only when its [control policy](https://www.prisma.io/docs/orm/contract-authoring/psl-syntax#control-policy) is `managed` in both contracts. Because nothing is asked, a `--delete` for such a change fails with `CLI.CONSENT_UNUSED`. If you want 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`. Some changes cannot be made in one step, and are refused with `MIGRATION.PLANNING_FAILED`: - two models or fields that trade tables or columns; - a model or field that moves to a name another one gives up in the same change; - a model or field that takes over the table or column of one the change removes; - enum members that trade stored values; - an enum member that takes the stored value of a member the change removes. Make each of these changes in two steps, applied one after the other. For a trade, or a name another one gives up, go through a name nothing uses, such as `prisma_swap_users`. For a takeover, first remove the model, field or enum member that holds the name, then add the new one in the second step. If that first step removes an enum member and you answer `--delete`, a required column cannot keep the rows that hold the member: delete those rows, or set them to a value the enum keeps, before the step is applied. On MySQL and MongoDB, a changed stored name is handled as it was on every database before `8.0.0-rc.25`: a changed `@@map` is planned as dropping the table and creating an empty one, and asks for `--delete`. `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 ` 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. 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. A namespace is a PostgreSQL schema that your contract declares with a `namespace` block. When you remove that block, the command asks about each model in the namespace, as it asks about any removed model. Answer with the flag that matches what you meant: - If you renamed the namespace, answer with `--rename billing:finance`. Nothing is dropped. - To keep one model's data, move it to a namespace the contract keeps, as in `--rename billing.Invoice:public.Invoice`. - To drop the namespace, answer with `--delete billing`. This answers for every model in it, and drops its tables and the schema. The schema can also hold objects that no contract declares. A table created by hand is asked about by its storage name, as in `--delete billing.audit_log`, because `--delete billing` does not answer for it. Any other such object, such as a view or a function, makes the run fail before it changes anything. The error names each object, so drop or move it yourself, then run the command again. 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. On PostgreSQL and SQLite, removing a member from an `enum` block is asked about too, because rows may hold its value. The update asks once for each value, however many columns hold it: - 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. - 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. 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. A member that takes the stored value of a member you remove is refused too; remove the old member in one run and add the new one in the next. 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. On PostgreSQL and SQLite, a field of a [value object](https://www.prisma.io/docs/orm/contract-authoring/psl-syntax#value-objects) is asked about too when the contract renames it, removes it, or makes it required. Name the field through its value object, such as `GeoPoint.lat`, not through a model. Each statement acts on every column that holds the value object, including lists of it and value objects nested in another: - `--rename GeoPoint.lat:GeoPoint.latitude` renames the key in every stored object. - `--delete GeoPoint.lng` removes the key from every stored object. - An enum value inside a value object is asked about as the enum value. `--rename Status.ARCHIVED:Status.RETIRED` rewrites it wherever it is stored, and `--delete Status.DRAFT` sets an optional field that holds it to JSON `null` and takes it out of a list. - A field that becomes required gets a temporary value, such as `""`, `0`, `false` or `[]`, in every stored object that lacks the key or holds JSON `null` there, when the field's type takes one. A stored object that already holds a value keeps it. A field whose type takes none of them, such as a `DateTime`, is refused. To fill it with a value of your own, plan the change with [`migration plan --backfill`](https://www.prisma.io/docs/cli/migration-plan#renames-and-data-loss) instead. A [variant](https://www.prisma.io/docs/orm/contract-authoring/psl-syntax#base-models-and-variants) whose discriminator value changes, or that the contract removes, is asked about as a model: - `--convert Bug` rewrites the old value to the new one in the base table's discriminator column. - `--rename Bug:Defect` of a variant whose value also changed rewrites the value in the same way. - `--delete Bug` deletes the rows that hold the old value, then drops a removed variant's own table or the columns only it used. 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`](https://www.prisma.io/docs/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. 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. A new required field on MongoDB is filled with a temporary value in the documents that lack it. The exception is a new required field that a unique index covers: it gets no temporary value, because every document would get the same one, so on a collection that has documents the apply stops. Plan that change with `migration plan --backfill` instead, as the error says. A change to a collection's validator checks the documents first and stops with `MIGRATION.PRECHECK_FAILED` while one does not fit; fix those documents, then run `db update` again. See [how migrations work](https://www.prisma.io/docs/orm/migrations/how-migrations-work#the-same-model-for-sql-and-mongodb). This paragraph and the one before it apply since `8.0.0-rc.24`. On MongoDB, a model rename renames its collection and keeps its documents, indexes and validator, and a field rename renames the field in every document that holds it. `--rename` and `--delete` name fields by their names in the schema, not by the names they are stored under. Removing a field from a model removes it from every document that holds it, so the update asks about it: answer with `--delete .`, or with `--rename`. Without a stored contract, the field is named by its collection, as in `--delete profiles.nickname`. On PostgreSQL and SQLite, a type change that can lose values is answered with `--delete `, which lets the database's own cast run; it may cut or round values, and a value it cannot cast fails the update. To write the conversion yourself, use [`migration plan --convert`](https://www.prisma.io/docs/cli/migration-plan#type-changes-and-required-fields) instead. ## Recommended flow [#recommended-flow] #### npm ```bash npx prisma contract emit npx prisma db update --db "$DATABASE_URL" --dry-run npx prisma db update --db "$DATABASE_URL" --advance-ref db npx prisma db verify --db "$DATABASE_URL" ``` #### pnpm ```bash pnpm prisma contract emit pnpm prisma db update --db "$DATABASE_URL" --dry-run pnpm prisma db update --db "$DATABASE_URL" --advance-ref db pnpm prisma db verify --db "$DATABASE_URL" ``` #### yarn ```bash yarn prisma contract emit yarn prisma db update --db "$DATABASE_URL" --dry-run yarn prisma db update --db "$DATABASE_URL" --advance-ref db yarn prisma db verify --db "$DATABASE_URL" ``` #### bun ```bash bunx prisma contract emit bunx prisma db update --db "$DATABASE_URL" --dry-run bunx prisma db update --db "$DATABASE_URL" --advance-ref db bunx prisma db verify --db "$DATABASE_URL" ``` Use `--dry-run` before applying changes in shared environments. ## Data lines [#data-lines] `db update` prints nothing on standard output. Its exit code is the result, and `--json` carries the plan and what ran. The `--json` output is one JSON object per line, and the result is on the line whose `kind` is `result`: ```bash npx prisma db update --db "$DATABASE_URL" --dry-run --json | jq 'select(.kind == "result") | .envelope.result.dataLoss' ``` Warnings about the planned change are in the result's `plannerWarnings`, a list of objects with `kind`, `summary` and `why`, as in `migration plan --json`. Before `8.0.0-rc.24`, `db update` and `db init` listed them under `warnings`. [Global flags](https://www.prisma.io/docs/cli/global-flags#data-lines-of-prisma-orm-commands) lists the data lines of the other commands. ## When to use migrations instead [#when-to-use-migrations-instead] For reviewable database changes in version control, use [`migration plan`](https://www.prisma.io/docs/cli/migration-plan) and [`db migrate`](https://www.prisma.io/docs/cli/db-migrate). Use `db update` on any database whose migration history does not need to be kept, such as a local, development, preview, or staging database, shared or not. A database that changes only through migrations, such as production, must not use it, because it leaves no migration behind. ## Related pages - [auth](https://www.prisma.io/docs/cli/auth): Sign in to your Prisma account from the CLI, sign out, and manage workspace sessions. - [branch](https://www.prisma.io/docs/cli/branch): List platform branches for a project. - [bucket](https://www.prisma.io/docs/cli/bucket): Create and manage object-store buckets. - [Configuration](https://www.prisma.io/docs/cli/configuration): Configure Prisma ORM CLI commands with prisma.config.ts and global flags. - [contract emit](https://www.prisma.io/docs/cli/contract-emit): Emit Prisma ORM contract artifacts.