While working on a Rails application, we all have had to in some way. You can change the and the , as well as changing the column with the . change the database column column name column type type conversion Changing the Column Name Before we can change a column, we have to create it. Let's start with for profiles. creating a model create_table t.references t.string t.string , < ActiveRecord::Migration class CreateProfiles def change :profiles do |t| :user :avatar :name null: false end end end Once you run the profiles data will be migrated to the database. Then we decided to change the . rake db:migrate column name How can we do this? There are 3 different ways to achieve that: Making a migration. new Fixing the migration. existing Making a migration in order to the table. change Be sure to run rake db :migrate at the end of all the examples, so the changes will be saved in the database. Making a New Migration to Change the Column Name First, you need to create a : new migration g migration rename_profile_name_to_full_name rails Then, add a in the newly generated file, specifying which table and what column you want to change: rename_column rename_column , , < ActiveRecord::Migration class RenameProfileNameToFullName def change :profiles :name :full_name end end And you are done! Fixing an Existing Migration Before we start, you should that you want to fix. Depending on the migration was made you can do it in the following ways: rollback the migration when The last migration: rake rollback db: Calling the migration version: rake down VERSION=YOUR_MIGRATION_VERSION db: migrate: Once the rollback has been completed you are free to edit the model. create_table t.references t.string t.string , < ActiveRecord::Migration class CreateProfiles def change :profiles do |t| :user :avatar :full_name null: false end end end Making a New Migration to Change the Table You should only use this option when adding multiple changes to the table at once. Start by creating a new migration: g migration change_profiles rails After the file has been created, specifying which column you want to change by adding a type on its place: rename change_table t.references t.string t.rename , < ActiveRecord::Migration class ChangeProfiles def change :profiles do |t| :user :avatar :name :full_name # t.string -> t.rename end end end Always run your after each change to the database to make it sure everything is working as intended. Also, please be careful with reserved names in Rails or SQL when renaming a column. Here is a list of to help you. RSpec tests reserved words Changing the Column Type This is a common change that encounters - you start with a having an attribute of one and then you realize it needs to be another. At first, you might think that you should store a as a text when it should actually be a string. every programmer model type profile name You can easily fix this by generating a : Don't worry! new migration rails g migration change ame to be string rofiles _n _ _ _ _ in _p Now, open up and edit it in the following way: the migration file change_column , , < ActiveRecord::Migration class ChangeNameToBeStringInProfiles def change :profiles :name :string end end Note that . It will cause an error if you try to rollback. To prevent this, modify the usual change method in the migration to use two separate like this: change_column is an irreversible migration up and down methods change_column , , change_column , , < ActiveRecord::Migration class ChangeNameToBeStringInProfiles def up :profiles :name :string end def down :profiles :name :text end end Start by creating a and change the to a string. Test this out! profile factory name type Changing the Column Type - Errors There are times when simply changing the column type will not work. When you try to write a migration that converts a string column to an integer: change_column , , :profile :age :integer Your database will complain and raise an error: PG::DatatypeMismatch: ERROR: "age" cannot be automatically HINT: Specify a expression the . column cast to type integer USING to perform conversion To solve this, we will follow the hint and specify in our migration how data should be converted. There are two ways to do this: change_column , , :profile :age 'integer USING CAST(age AS integer)' change_column , , , :profile :age :integer using: 'age::integer' That's it! Hope these steps helped you to change the database columns to your liking. Previously published at kolosek.com/rails-change-database-column/