When deploying to Railway, I'm getting this error:
89 migrations found in prisma/migrations
Error: P3009
migrate found failed migrations in the target database, new migrations will not be applied. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
The
20250414071928_14042025
migration started at 2025-04-16 07:08:08.960551 UTC failed
There is also already a newer migration that basically replaces this old migration mentioned in the erorr. What do I need to do to resolve it or roll it back?Patching & hotfixing | Prisma Documentation
How to reconcile the migration history after applying a hotfix or patch to a production environment.
11 Replies
Hi @mb23. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
You're encountering a common issue with Prisma migrations in a production environment. This error occurs when Prisma detects a failed migration in your database, preventing new migrations from being applied.
How to resolve the migration issue
When deploying to Railway, you need to resolve the failed migration before new migrations can be applied. Prisma provides a
migrate resolve
command for this purpose, but since you're using Wasp, you'll need to access this functionality.
There are two approaches to solving this:
Option 1: Use Prisma directly through Wasp
While Wasp doesn't currently have a direct command for resolving migrations in production, there's an open issue about providing direct access to Prisma commands through the Wasp CLI:
Give direct access to prisma CLI/commands through wasp CLI
Until this is implemented, you'll need to use a more manual approach.
Option 2: Connect to your production database and resolve the migration
1. Connect to your Railway PostgreSQL database using the Fly CLI (similar approach for Railway):
2. Once connected, you can examine the _prisma_migrations
table to see the status of your migrations.
3. For the failed migration, you have two options:
- Mark it as applied if you know the schema changes were actually applied
- Roll it back if you need to undo the changes
Since you mentioned there's a newer migration that replaces the failed one, you might want to mark the failed migration as applied if the schema is already in the correct state.
For future deployments
When deploying to Railway, it's recommended to run database migrations as a separate step before deploying your application, rather than as part of the container startup:
We should run prisma migrate deploy
as a step before deployment
For Railway specifically, when making updates and redeploying:
1. Run wasp build
to rebuild your app
2. Run railway up
in the .wasp/build
directory (server)
3. Repeat all the steps in the .wasp/build/web-app
directory (client)
Deploying Manually - Updates & Redeploying
If you continue to have issues, you might need to connect to your production database and manually resolve the migration state to match your current schema.
Kapa.ai: I'm an AI assistant specialized in answering questions about Wasp. If you need more specific help with your migration issue, please provide additional details about your setup and the exact error messages you're seeing.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @mb23, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?@kapa.ai The new migration that replaces the failed one is not deployed yet. Only locally.
Let me check the knowledge base for you again. One moment! :books:
----------------------
Thanks for clarifying. Since the new migration that replaces the failed one is only local and not deployed yet, you'll need to resolve the failed migration in your production database before you can proceed.
When Prisma encounters a failed migration, it won't apply any new migrations until you resolve the issue. Here's what you can do:
Resolving the failed migration
1. Connect to your production database on Railway. You can use a similar approach to what's done with Fly:
(For Railway, you'll need to use their equivalent command or connection method)
2. Once connected, you can examine the
_prisma_migrations
table to see the status of your migrations.
3. For the failed migration (20250414071928_14042025
), you have two options:
- Mark it as applied: If you believe the schema changes were actually applied despite the error
- Roll it back: If you need to completely undo the changes
Since Wasp doesn't currently have a direct command for resolving migrations in production (there's an open issue about this), you'll need to handle this manually.
Manual resolution options
1. If the migration partially succeeded and the database is in a good state:
You can mark the migration as applied in the _prisma_migrations
table by updating its status.
2. If the migration failed completely and you need to roll back:
You may need to manually revert any partial changes and then mark the migration as resolved.
After resolving the failed migration, you should be able to deploy your new migration that replaces the failed one.
Remember that for future deployments, it's recommended to run database migrations as a separate step before deploying your application, as discussed in this GitHub issue.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @mb23, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?@kapa.ai How exactly can I mark the migration as applied?
Let me check the knowledge base for you again. One moment! :books:
----------------------
To mark a failed Prisma migration as applied in your Railway database, you'll need to connect to your production database and update the status in the
_prisma_migrations
table.
Here's how to do it:
1. First, connect to your Railway PostgreSQL database. Based on the knowledge sources, you can use a similar approach to what's done with Fly:
For Railway, you'll need to use their equivalent connection method. You might be able to use the Railway CLI or dashboard to access your database.
2. Once connected to your database, you can update the status of the failed migration in the _prisma_migrations
table:
This SQL command marks the migration as fully applied by setting the applied steps count equal to the total steps count and setting a finished timestamp.
3. After executing this command, you should be able to run your migrations again without the error.
Remember that when working with production databases, it's always a good idea to:
1. Make a backup before making manual changes
2. Verify the state of your database after making changes
3. For future deployments, consider running migrations as a separate step before deployment as mentioned in this GitHub issue
If you're uncomfortable making direct SQL changes to your production database, you might want to consider recreating your database if that's an option for your project.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @mb23, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?@kapa.ai How can I exactly roll it back in Railway? Somehow, I think I accidentally removed a relevant column for the user entity which caused an issue.
Let me check the knowledge base for you again. One moment! :books:
----------------------
Based on your situation where you accidentally removed a relevant column for the user entity in your Prisma schema, rolling back a failed migration in Railway requires a manual approach since Wasp doesn't currently provide direct commands for this.
Here's how you can roll back the failed migration in Railway:
1. First, connect to your Railway PostgreSQL database. You'll need to use Railway's connection method, which is similar to this approach used with Fly:
Or use the connection details from your Railway dashboard to connect using a PostgreSQL client.
2. Once connected, you need to:
a) Mark the failed migration as rolled back in the
_prisma_migrations
table:
b) Manually restore the column you accidentally removed. This requires knowing the exact column definition:
3. After manually fixing the database schema, you can mark the migration as resolved:
This is a delicate operation, so I strongly recommend:
1. Making a backup of your database before attempting these changes
2. If possible, test these operations in a staging environment first
3. Consider if recreating the database might be simpler if you don't have important production data
For future reference, when working with Prisma in Wasp, it's important to be careful with schema changes, especially removing columns. The issue you're experiencing is related to how Prisma handles migrations, as seen in several GitHub issues where users encountered similar database migration problems.
If you continue to have issues, you might need to consider a more comprehensive approach to fixing your database schema to match your current application requirements.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @mb23, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?@kapa.ai Ok, now, I see my real problem. I'm developing another Wasp app and have it in Railway as well. I accidenally deployed the wrong server and database for this project 😄 It's not a huge problem since there are no users yet but how can I deploy the right server/database again?
Let me check the knowledge base for you again. One moment! :books:
----------------------
I understand your situation - you accidentally deployed the wrong server and database for your Wasp project to Railway. Since there are no users yet, this is definitely fixable. Here's how you can deploy the correct server and database:
Deploying the correct server and database to Railway
You have two main options:
Option 1: Delete and redeploy
1. Go to your Railway dashboard and delete the incorrectly deployed services (both server and PostgreSQL database)
2. Follow the manual deployment steps for Railway:
- Build your correct Wasp app:
- Create a new PostgreSQL instance in Railway
- Create empty services for your server and client
- Set up domains for both services
- Deploy your server and client following the Railway deployment steps
Deploying Manually - Railway provides detailed instructions for this process.
Option 2: Update the existing deployment
If you prefer to keep the same Railway project and just update it:
1. Make sure your correct Wasp app is built:
2. Navigate to your app's build directory:
3. Link to your existing Railway project:
4. Update your environment variables in the Railway dashboard if needed
5. Deploy the correct server:
For the database, you'll need to:
1. Either use the existing database and run migrations to update the schema
2. Or create a new database instance and update the
DATABASE_URL
environment variable
For future deployments
When making updates and redeploying:
1. Run wasp build
to rebuild your app
2. Run railway up
in the .wasp/build
directory (server)
3. Repeat the steps in the .wasp/build/web-app
directory (client)
Deploying Manually - Updates & Redeploying
Since you mentioned there are no users yet, starting fresh with Option 1 might be the cleanest approach to ensure you have the correct setup.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @mb23, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?@kapa.ai can I also only redeploy the database?
Let me check the knowledge base for you again. One moment! :books:
----------------------
Based on the knowledge sources, there isn't specific information about redeploying only the database component of a Wasp app on Railway. However, I can provide some guidance based on how Railway and Wasp work together.
When using Railway with Wasp, your database is deployed as a separate service from your server and client. This means you can indeed work with just the database component if needed.
To redeploy or update just the database:
1. You can access your PostgreSQL database directly from the Railway dashboard
2. If you need to update the schema, you would run migrations against this database
For running migrations specifically, you would use Wasp's database commands:
This command synchronizes the development database with the current state of your schema (entities) and generates new migrations if there are changes in the schema Wasp CLI Database Commands.
Since you're working with a production database on Railway, you might need to connect to it directly to apply changes or run migrations. Railway provides ways to connect to your PostgreSQL instance through their dashboard.
If you're trying to reset or recreate just the database while keeping the same server deployment, you could:
1. Create a new PostgreSQL instance in Railway
2. Update the
DATABASE_URL
environment variable in your server service to point to the new database
3. Run migrations to set up the schema in the new database
This approach would allow you to effectively "redeploy" just the database component of your application.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @mb23, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?