SwitchUp SwitchUp Ranked Best Coding Bootcamps 2025

Fixing database migration files

Written in Ruby on Rails
updated on 10 Jan 2023

Like we have mentioned, in order to modify the representations in the database, we have to use migration files to do so.

For example, you have a User table in the database with first_name and last_name.

Now, you want to also add the email_address field in the table. What do you do? Well, these should be the steps
  1. Run rails g migration add_email_address_to_users to create an empty migration file
  2. In the new migration file db/migrate/xxx_add_email_address_to_users.rb, add the following:
class AddEmailAddressToUsers < ActiveRecord::Migration def change add_column :users, :email_addrss, :string end end
Then we run, rails db:migrate to complete the modification to the database. All done!

But, Oops! There was a typo in the migration file. email_addrss should have been email_address.

Now, here's an important catch. You cannot just modify the original file and run rails db:migrate to fix this. Why? Because the modification has already been done to the database according to the original migration file. What you need to do is to undo the modification first.

So, here should be the steps:
  1. Run rails db:rollback to rollback the previous 1 migration
  2. Fix the typo in the migration file
  3. Run rails db:migrate again

If you made a huge mess in all your migration files, and you don't mind destroying your database, you can do these:
  1. Run rails db:drop to destroy the whole database
  2. Run rails db:create to create a new database
  3. Run rails db:migrate

Trusted by

Students and instructors from world-class organizations

Join the upcoming Cohort #120

Enroll for December 7th, 2026