Replies: 4 comments
|
This is one of the places where Prisma Migrate is intentionally conservative: it diffs the Prisma schema, but it cannot always know that a model rename is the same database table rather than “drop one model, create another”. For a pure Prisma-level rename where the underlying table should keep its old name, use mapping: model Account {
id Int @id @default(autoincrement())
name String
@@map("User")
}That renames the model in Prisma Client while keeping the database table as If you really want to rename the physical table too, create the migration and edit the generated SQL before applying it: ALTER TABLE "User" RENAME TO "Account";Same idea for columns: use I agree this is something you have to be very careful with. The safe workflow is: generate migration, review the SQL, replace destructive drop/create statements with rename statements when it is a rename, then apply. Never deploy a generated migration involving renames without reading it. |
|
Small categorization note: this is probably better tracked as a feature request or migration safety discussion than as a normal question. The behavior is frustrating, but the root cause is that Prisma Migrate is doing schema diffing, not semantic refactoring detection. When the old model name disappears and a new model name appears, Prisma cannot reliably know whether that was intended to be a rename, a table replacement, a split, a merge, or an intentional drop and recreate. So the generated migration represents what the diff engine can prove: one table removed, one table added. That said, I agree that this is a dangerous footgun, especially because the safe intent is common. A model rename is not an exotic operation, and a generated migration that drops a populated table is something teams should be forced to notice before it gets anywhere near production. The current safe workflow is to treat renames as migrations that require human edited SQL. For example, if the generated migration contains this kind of change: DROP TABLE "User";
CREATE TABLE "Account" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
CONSTRAINT "Account_pkey" PRIMARY KEY ("id")
);then the migration should be edited to a real table rename instead: ALTER TABLE "User" RENAME TO "Account";For a column rename, the same idea applies: ALTER TABLE "Profile" RENAME COLUMN "biograpy" TO "biography";If the goal is only to rename the Prisma Client API while keeping the physical database table name unchanged, use mapping instead: model Account {
id Int @id @default(autoincrement())
name String
@@map("User")
}And for fields: model Account {
id Int @id @default(autoincrement())
fullName String @map("name")
@@map("User")
}That avoids changing the database object name at all. Prisma Client sees For production use, I would not rely on generated migrations being safe by default. The safer policy is: The CI idea you described is a good direction. A migration verification job that applies the already deployed baseline, seeds deterministic data, applies only the new migrations, and then compares before and after state would catch exactly this class of issue. The rename similarity scoring is also useful because it can distinguish “this looks like an intentional drop” from “this looks like the same table under a new name.” I would frame the requested improvement as two separate things: 1. Rename intent support Prisma would need some way to express or infer rename intent, for example an interactive prompt during migration generation or an explicit rename annotation/workflow. Without that, the engine has no reliable semantic signal. 2. Stronger destructive migration guardrails Even if automatic rename detection is not implemented, dropping a table or column that may contain data should be surfaced very loudly during migration generation, and teams should have a first class way to fail CI on destructive operations. So the practical answer today is: yes, manually edit the migration or use |
|
This is intentional (though frustrating). Prisma Migrate diffs the schema declaratively — it sees "model A is gone, model B is new" rather than "model A was renamed to B". It cannot infer renames. The workflow Prisma expects:
-- Replace this:
-- DROP TABLE "OldName";
-- CREATE TABLE "NewName" (...);
-- With this:
ALTER TABLE "OldName" RENAME TO "NewName";This is documented but easy to miss. The For field/column renames, same pattern: ALTER TABLE "MyTable" RENAME COLUMN "oldName" TO "newName";It's not ideal that the default generated migration is destructive, but the reasoning is that Prisma can't reliably distinguish a rename from a delete+create at the schema diff level. Always use |
|
Yeah this is expected, even though it's a nasty footgun. Prisma Migrate is declarative, it diffs the desired schema against the current one, but it has no way of knowing you renamed The intended way to do any rename is to generate the migration but not run it, then edit the SQL yourself: prisma migrate dev --name rename_foo_to_bar --create-only
-- what Prisma generated
-- DROP TABLE "Foo";
-- CREATE TABLE "Bar" (...);
-- what you actually want
ALTER TABLE "Foo" RENAME TO "Bar";
-- or for a column
ALTER TABLE "Bar" RENAME COLUMN "oldName" TO "newName";Then apply it with Rule of thumb I go by: any rename gets If that sorts it, a tick as the accepted answer would be appreciated. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Question
When you rename a model in your Prisma schema (and likely also when renaming fields/columns), running
prisma migrate devgenerates a migration that issues aDROP TABLEon the old table name followed by aCREATE TABLEfor the new name... instead of anALTER TABLE ... RENAME TOstatement.This means that if this migration is applied to a production database (e.g. via
prisma migrate deploy) without careful manual review, all existing data in that table is silently and permanently deleted. There is no warning, no prompt, and no safeguard built into the migration process to prevent this from happening.So therefore I'm asking again, Who in their right minds where thinking this was a good idea??
How to reproduce (optional)
prisma migrate devto apply the initial migration.schema.prisma:prisma migrate devagain and inspect the generated SQL.Observed: The migration contains
DROP TABLE "User"followed byCREATE TABLE "Account".Expected: The migration contains
ALTER TABLE "User" RENAME TO "Account".Expected behavior (optional)
Prisma Migrate should detect that a model rename has occurred and generate an
ALTER TABLE ... RENAME TO ...statement, preserving all existing data. At a minimum, if Prisma cannot reliably detect renames, it should:DROPto confirm destructive operations).The current behavior is a silent data loss footgun with no guardrails for production use.
Migration Data-Loss Detection Pipeline + Generic Explanation
A CI pipeline that runs on every pull request that modifies migration files, before those migrations are ever applied to a real environment. It is entirely self-contained, no access to production or staging databases is required.
The pipeline works in two passes against a fresh, isolated database:
Pass 1: Establish baseline
Pass 2: Apply and verify
What Gets Detected
DROP+CREATEfor renames, the comparison sees a table/column disappear and a new one appear. A structural similarity scorer is run over every (disappeared, appeared) pair using signals like:If the similarity score crosses a threshold (≥ 60 %), the pair is flagged as a probable rename candidate rather than silently treated as an intentional drop. The migration is still blocked: the developer must resolve it by either adding
@@map/@mapto the Prisma schema (which causes Prisma to emit a properRENAMEinstead) or manually editing the migration SQL to useALTER TABLE ... RENAME.Output
A coverage report is produced showing every table with:
Severity levels:
Why This Matters
The standard Prisma workflow gives no feedback about data loss until the migration runs against real data. This pipeline moves that feedback to the pull request review stage, before anything reaches staging or production, and specifically surfaces the rename footgun that Prisma's generation creates by design.
All reactions