Rails Migration column_change

I just created a rails migration for one of my tables. Wanted to change the type of a column from string to integer. My migration looked like that:


class ChangeColumnTypeInConsultants < ActiveRecord::Migration
  def change
    change_column :consultants, :title_id, :integer
  end
end

Unfortunately it didn’t worked out. I got this Exception:

PG::Error: ERROR: column "title_id" cannot be cast to type integer
: ALTER TABLE "consultants" ALTER COLUMN "title_id" TYPE integer

And on pgAdmin I got this error:

ERROR: column "title_id" cannot be cast to type integer
SQL state: 42804

I couldn’t figure it out how to solve this problem. I just wrote 2 more migrations, for deleting the columns and adding them new with the correct data type.


class RemoveColumnsFromConsultants < ActiveRecord::Migration
  def change
    remove_column :consultants, :title_id
  end
end

and than for adding it new.


class AddColumnsToConsultants < ActiveRecord::Migration
  def change
    add_column :consultants, :title_id, :integer
  end
end

That worked for me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s