Skip to content

Ruby on Rails Database Migration Error Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Ruby on Rails Database Migration Error Fix. We cover key concepts, practical examples, and best practices.

The Problem

Running rails db:migrate fails with errors like PG::DuplicateTable: ERROR or Migrations are pending. The database schema does not match the migration files. This happens after pulling code from version control or switching branches.

Quick Fix

Step 1: Check migration status

rails db:migrate:status

Expected: Look for down migrations that should be applied.

Step 2: Run pending migrations

rails db:migrate RAILS_ENV=production

Step 3: Rollback the last migration

rails db:rollback STEP=1

Expected: Fix the migration file and re-run: rails db:migrate

Step 4: Reset the database

rails db:reset

Expected: Drops, recreates, and runs all migrations. Use only in development.

Step 5: Fix schema conflicts

rails db:migrate:reset

Expected: Regenerates schema.rb.

Prevention

  • Run migrations in the correct order after pulling code.
  • Never modify existing migrations after they are merged.
  • Use db:migrate:status to check state before running.

Common Mistakes with migration error

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world RAILS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does migration fail after pulling new code?

New migration files exist but the database is out of sync. Run rails db:migrate to apply them.

Can I edit an existing migration?

Only if it has not been applied to any environment. If already applied, create a new migration to make changes.

How to handle migration conflicts in a team?

Each developer should create unique timestamped migrations. Run rails db:migrate after each pull to stay in sync.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro