Hello @lumenwrites
npx prisma db push is meant for development use only. The development db is considered "resettable" and "seedable" as its used to develop your migrations and to test/tweak them if needed. Looks like you have used it in your production environment. In production, you should use npx prisma migrate deploy and it would ensure your db won't get reset in any case.
Now in order to resolve your current situation we need to tweak the migrations history using some primitives which migrate provides like the diff command(https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-diff)
- You will need to generate a new migration which will be the difference between migrations history stored in your disk in the migrations folder and your production database. The
--to-schema-datasourceflag here point to the database stored in your.envfile but you can also change this flag to--to-urlif you prefer to give the database url in the command itself.
mkdir -p prisma/migrations/add-comments-and-post/
npx prisma migrate diff --from-migrations prisma/migrations --to-schema-datasource prisma/schema.prisma --shadow-database-url --script > prisma/migrations/add-comments-and-post/migration.sql
The --shadow-database-url parameter is required and you will need to point it to any empty postgres database either a local one or hosted one. Prisma will use that to reply migration history and calculate the diff.
This will create a new migration in prisma/migrations/add-comments-and-post/migration.sql which will have the SQL for the model you have added via db push.
- Now you have the migration ready, we will need to mark it as applied. This is because those tables already exists in the database and we can't run the actual
CREATEsql statements to the database. We will just mark the migration as applied as it was already applied when you rannpx prisma db push.
npx prisma migrate resolve --applied add-comments-and-post
- Now, you should be ready to go. Run
npx prisma migrate devonce now and it should say everything is in sync.
Difference between prisma db push and prisma migrate dev - Stack Overflow
`npx prisma db push` and `npx prisma migrate` error when there is a table that starts with `_`
database - Why does the command npx prisma db push not progress and fail to push my models to Supabase? - Stack Overflow
database - Why can't 'npx prisma db push' find my prisma schema? - Stack Overflow
They serve two different environments. The prisma db push is not to be used in your production environment, as stated in the docs
db pushuses the same engine as Prisma Migrate to synchronize your Prisma schema with your database schema, and is best suited for schema prototyping. Thedb pushcommand:
Introspects the database to infer and executes the changes required to make your database schema reflect the state of your Prisma schema.
By default, after changes have been applied to the database schema, generators are triggered (for example, Prisma Client). You do not need to manually invoke
prisma generate.If
db pushanticipates that the changes could result in data loss, it will:
- Throw an error
- Require the
--accept-data-lossoption if you still want to make the changesNote:
db pushdoes not interact with or rely on migrations. The migrations table will not be updated, and no migration files will be generated.
The prisma migrate dev is used in you local environment, as explained in the docs
migrate devis a development command and should never be used in a production environment.This command:
- Replays the existing migration history in the shadow database in order to detect schema drift (edited or deleted migration file, or a manual changes to the database schema)
- Applies pending migrations to the shadow database (for example, new migrations created by colleagues)
- Generates a new migration from any changes you made to the Prisma schema before running
migrate dev- Applies all unapplied migrations to the development database and updates the
_prisma_migrationstable- Triggers the generation of artifacts (for example, the Prisma Client)
The
migrate devcommand will prompt you to reset the database in the following scenarios:
- Migration history conflicts caused by modified or missing migrations
- The database schema has drifted away from the end-state of the migration history
If you have any other question regarding this, there is this comparison in the docs explaining when to use one or the other.
- Prisma db push syncs (and formats) your prisma schema to that of your database schema.
- While prisma migrate dev "command automatically generates SQL migration files (saved in /prisma/migrations) and applies them to the database".
The main difference between both is the generation of the migration files.
I would recommend you to pass the schema file location while invoking npx prisma db push command.
npx prisma db push --schema='/location/to/schema.prisma'
Also, I would recommend you to run the command from root location of your project.
Here's a reference for db push command.
npx prisma db push not working or not responding with next js
Try shutting down the other server of next js and then again run npx prisma db push. It worked for me
Hey guys, i am writing this is pain but i am a startup and we had around 500+ data, i was pushing a change in database and got my data erased. I am stupid that i didnt do the migrations and is free version. I have mailed the supabase community but i would like your help too.
mastan@mastan-Lenovo-ideapad-110-15ISK:~/Desktop/tata/sas$ npx prisma db push
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": MySQL database "discord-clone" at "aws.connect.psdb.cloud"
Error: P1001: Can't reach database server at `aws.connect.psdb.cloud`:`3306`
Please make sure your database server is running at `aws.connect.psdb.cloud`:`3306`.