Hi @rainnfx 👋
Thank you for raising this question.
It's not a stupid question at all! Managing schema changes without data loss is a common concern. The prisma db push command is designed to quickly synchronize your Prisma schema with your database schema. However, it does not generate migration files and is best suited for prototyping in a development environment. When prisma db push detects changes that could result in data loss (e.g., adding a required field without a default value), it will prompt you to reset the database.
You should use prisma migrate commands instead of prisma db push. This command helps you create and apply migrations during development. This command helps you create and apply migrations during development.
- Expand and Contract Pattern:
This pattern allows you to evolve your schema without downtime and data loss. It involves making non-destructive changes first (e.g., adding a nullable field) and then gradually evolving the schema.
You can read more about this pattern in the documentation.
Go into your schema and make the change:
NormalizedName String @unique @db.VarChar(64)
NormalizedName String? @unique @db.VarChar(64)
Then create a draft migration:
$ npx prisma migrate dev --name migration-name --create-only
Then edit the migration in SQL (this allow null values ie optional)
ALTER TABLE myTable ALTER COLUMN myColumn {DataType} NULL;
OR Postgres
ALTER TABLE myTable ALTER COLUMN myColumn DROP NOT NULL;
etc.
Then apply the modified SQL:
$ npx prisma migrate dev
I hope this works for you :)
In a development environment, Prisma Migrate sometimes prompts you to reset the database. Resetting drops and recreates the database, which results in data loss. The database is reset when:
- You call
prisma migrate resetexplicitly - You call
prisma migrate devand Prisma Migrate detects drift in the database or a migration history conflict
I'm not sure why Prisma thinks that your change is breaking, but there is probably no other way to make schema change without data loss.
To recreate your database data consider using seeding script
If you are prototyping, consider using the db push command, although it will still result in data reset if Prisma considers that the change is breaking.
How do I use Prisma without having to wipe my database all the time?
prisma migrate dev - data lost
What is the good practice for migrations and keeping them on GitHub?
Prisma model change without data loss
I'm using Prisma for a large project I've been working on for the past few months, and I still don't understand the flow of it. At first I was migrating every time I made a change to my schema, and constantly getting the "All data will be lost" prompt. Since then I've learned to use `db push` while developing and then migrate when a feature is finished, but it's still constantly making me reset my database.
Right now I'm using the same db for dev and staging (because I don't want to pay for 2 different ones) but I'd imagine this would still be an issue if I had separate databases. What do I need to do to stop being forced to reset my db? Or, at least, how would I migrate my development db to mirror my production db without it being wiped?
As you can probably tell, this is my first time working with Prisma (or any ORM, in depth) so this might be a stupid question, but I can't find much online besides general tutorials. Any help would be greatly appreciated!