iTranslated by AI
Forcing a DB Reset in Prisma 2
In Prisma 2, using prisma db push --force --preview-feature during development allows you to synchronize the database while ignoring migrations.
This is convenient during development.
However, if data already exists and you make a change that breaks consistency (such as adding a required field), you may encounter an error.
datasource db {
provider = "postgres"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
// ↓ Add a required field
someRequiredValue String
}
$ prisma db push --force --preview-feature
Error:
⚠️ We found changes that cannot be executed:
• Added the required column `someRequiredValue` to the `User` table without a default value. There are 1 rows in this table, it is not possible to execute this migration.
Unfortunately, Prisma currently does not have a built-in DB Reset feature.
Workaround using a dummy reset schema
I discovered that you can work around this by preparing a separate schema specifically for resetting.
For example, create a file named something like prisma/reset.prisma with the following content:
// prisma/reset.prisma
datasource db {
provider = "postgres"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model Reset {
id Int @id @default(autoincrement())
}
Run this by providing the --schema parameter:
$ prisma db push --preview-feature --force --schema=./prisma/reset.prisma
This will cause other tables in the DB to be deleted, leaving only the Reset table.
After that, you can simply run $ prisma db push --force --preview-feature again.
If necessary, you can add it to your package.json as follows:
"scripts": {
"db:reset": "prisma db push --preview-feature --force --schema=./prisma/reset.prisma; prisma db push --force --preview-feature"
}
Discussion