Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this migration irreversible? (change_table, rename, text)

Tags:

I have what I think is a pretty simple migration. For some reason I get an IrreversibleMigration error when I try to db:rollback or db:migrate:redo.

The migration runs smoothly, but I'd rather keep it reversible. I can't figure out why it's not as written. Any ideas?

Here's the migration:

class AddWhyHypAndWhyHypeToStatements < ActiveRecord::Migration   def change     change_table :statements do |t|       t.rename :description, :why_hypocritical       t.text   :why_hypothetical     end   end end 

If it matters, "description" column is a text column. I'm using Rails 3.1/Ruby 1.9.2/PostgreSQL. Thanks for any help.

like image 469
Chris Humphreys Avatar asked Jan 23 '12 23:01

Chris Humphreys


People also ask

What is irreversible migration?

The migration that cannot be undone: Irreversible Migration.

What is reversible migration?

Introduced in Rails 4.0, reversible makes it possible to tell a migration using change (instead of up and down ) how to reverse migrations that Active Record doesn't know how to reverse by default, so that you can specify code to be executed whether migrating forward or rolling back, even inside a migration implemented ...

Can I rename migration file rails?

You can change the migration file name, but you have to perform a few steps: rake db:rollback to the point that queries table is rolled back. Now change the name of migration file, also the contents. Change the name of any Model that may use the table.

What is reversible migration in rails?

Reversible Migrations Rails allows us to rollback changes to the database with the following command. rails db:rollback. This command reverts the last migration that was run on the database. If the migration added a column event_type then the rollback will remove that column.


1 Answers

Looks like Rails has troubles reverting change_table method. Try doing it that way instead:

class AddWhyHypAndWhyHypeToStatements < ActiveRecord::Migration   def change     rename_column :statements, :description, :why_hypocritical     add_column :statements, :why_hypothetical, :text   end end 

You can see the list of commands that can be inverted in the docs or in Rails Guides.

like image 113
KL-7 Avatar answered Sep 27 '22 20:09

KL-7