dag729
dag729

Reputation: 199

How to change part of the schema in Ruby on Rails?

I was trying to change a line of schema.rb (t.string must become t.text) but I can't change it directly through the file because when I use rake db:migrate the changes revert on the previous state.

How can I change that string?

EDIT: If I didn't misunderstood something, I must edit the file inside db/migrate. In my case is 2010110801532_create_posts.rb, where exactly I'll have to put the change_column part?

This is my 2010110801532_create_posts.rb

class CreatePosts < ActiveRecord::Migration
  def self.up
    create_table :posts do |t|
      t.string :nome
      t.string :titolo
      t.text :contenuto

      t.timestamps
    end
  end

  def self.down
    drop_table :posts
  end
end

Upvotes: 3

Views: 6525

Answers (3)

Mark Thomas
Mark Thomas

Reputation: 37517

Step-by-step:

First, generate a new migration:

./script/generate migration change_string_to_text

In the generated migration file, nnnnnnnnnnn_change_string_to_text.rb, you need to add the line:

change_column :table_name, :column_name, :text

Then do a rake migrate to apply the changes to the database.

Upvotes: 4

Dan Harper
Dan Harper

Reputation: 1130

The schema.rb file is auto generated by rake, so any changes you make will be overwritten. As Matchu mentioned, a migration is the way to go, start with:

./script/generate migration change_string_to_text

Upvotes: 0

Matchu
Matchu

Reputation: 85832

Make a migration to change it :) If you're using migrations, then it's not a good idea to edit the schema file directly.

change_column :table_name, :column_name, :text

Upvotes: 3

Related Questions