Rahul Goyal
Rahul Goyal

Reputation: 774

Does rename_column in Rails ActiveRecord Migration preserve the old column data type?

I am trying to run a rails migration where in I am changing just the column name. So, this rails takes care of keeping the same data type as the old column or tries to make a guess based on the column content?

def change
   rename_column :marks, :percent, :percentage
end

The old type was decimal and after migration it still remains decimal. Just I wanted to know the type was preserved because the existing values were decimal or it was because of the old data type.

Upvotes: 0

Views: 939

Answers (2)

Awais Shafqat
Awais Shafqat

Reputation: 546

Yes, rename_column is only renaming the name of column with 3 parameters. change_column would change the type

def change
   change_column :marks, :percent, :text
end

Upvotes: 0

power
power

Reputation: 1265

rename_column is meant for only simply changing the name of the column without thinking about the datatype.

In the given migration you are changing only the name from percent to percentage so rails doesn't care about the datatype and only changes the name of the column. There is no change in the datatype, it will intact as it is. whether it is decimal, float, string etc.

Upvotes: 2

Related Questions