Rails conditional migrations – ensure column does not exist

If you’re running into a rails migration exception, such as:

column “x” of relation “y” already exists

Then you can conditionally run the associated migration in the following way:

unless YourModel.column_names.include?("the_column")
  add_column(:your_models, :the_column, :boolean, :default=>true)

Of course, it is probably best to address the underlying workflow issues that caused this issue to occur in the first place. However, if you just want to move along to get along – the above is a great solution.

Leave a Reply

Your email address will not be published. Required fields are marked *