CLIORM

db update

Update a database to match the current Prisma ORM contract.

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 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.

Usage

npx prisma db update --db "$DATABASE_URL"

Options

OptionWhat it does
--db <url>Connects to the database.
--dry-runShows planned operations without applying them.
--to <contract>Updates to a specific contract state. Accepts a contract reference: a hash, a hash prefix, a ref name, a migration directory name, or <dir>^. 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 <old>:<new>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 and SQLite, 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.
--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.
--convert <Variant>On PostgreSQL and SQLite, rewrites the stored discriminator value of a variant whose value changed. Repeatable.
--allow <Model>Lets one operation change who can read or write the model's data. Repeatable.
--advance-ref <name>After the update, points the named 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 <path>Read this config file instead of ./prisma.config.ts.
--jsonPrints a machine-readable result.

The global flags, such as --no-interactive and --confirm, apply too.

Contract references

A ref name is a name you gave a contract state with migration ref set, such as prod. <dir>^ 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 lists the forms for every command.

What happens to the db ref

The 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 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

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 <subject> 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 <old>:<new> 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 <Model>; 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.

npx 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 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.

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.

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, 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:

  • 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, 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> 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.

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 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 instead.

A variant 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, --rename of a value object field or of a variant whose discriminator value changes is refused, and so is --convert of a variant.

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. 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 <Model>.<field>, 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 <Model.field>, 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 instead.

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"

Use --dry-run before applying changes in shared environments.

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:

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 lists the data lines of the other commands.

When to use migrations instead

For reviewable database changes in version control, use migration plan and 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.