Question

I have migration (created way back) that i need to change, it's the below:

20130923000732_create_questions.rb

I need to change

 t.string to --> t.text

how can i achieve this ?

I read along that i can create a new migration renaming the column, but i did not quite understand it.

Was it helpful?

Solution 2

Your app is in development yet, just open that migration in edtor, change it to text and run all your migrations again. Or write a migration that will update that field type.

First in you terminal:

rails g migration change_column_type_in_questions

Then in your migration file:

class ChangeColumnTypeInQuestions < ActiveRecord::Migration
  def change
    change_column :questions, :body, :text
  end
end

Migration will look for table questions and will update column body type without loosing data.

OTHER TIPS

If 20130923000732_create_questions.rb migration is your last migration you can rollback with:

rake db:rollback

Otherwise you can simply down your specific migration with VERSION:

rake db:migrate:down VERSION=20130923000732

After rollback your migration, change your migration file and migrate again.

Run rails generate migration change_string_to_text_in_questions then a new migration file will be created, with

def change
  end

method, now insert, change_column :table_name, :column_name, :type now your migration file should look like this,

def change
    change_column :table_name, :column_name,:type
end

After this, Save the Changes and run db:migrate

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top