Change a column type from Date to DateTime during ROR migration
Solution 1
First in your terminal:
rails g migration change_date_format_in_my_table
Then in your migration file:
For Rails >= 3.2:
class ChangeDateFormatInMyTable < ActiveRecord::Migration
def up
change_column :my_table, :my_column, :datetime
end
def down
change_column :my_table, :my_column, :date
end
end
Solution 2
Also, if you're using Rails 3 or newer you don't have to use the up
and down
methods. You can just use change
:
class ChangeFormatInMyTable < ActiveRecord::Migration
def change
change_column :my_table, :my_column, :my_new_type
end
end
Solution 3
In Rails 3.2 and Rails 4, Benjamin's popular answer has a slightly different syntax.
First in your terminal:
$ rails g migration change_date_format_in_my_table
Then in your migration file:
class ChangeDateFormatInMyTable < ActiveRecord::Migration
def up
change_column :my_table, :my_column, :datetime
end
def down
change_column :my_table, :my_column, :date
end
end
Solution 4
There's a change_column method, just execute it in your migration with datetime as a new type.
change_column(:my_table, :my_column, :my_new_type)
Solution 5
AFAIK, migrations are there to try to reshape data you care about (i.e. production) when making schema changes. So unless that's wrong, and since he did say he does not care about the data, why not just modify the column type in the original migration from date to datetime and re-run the migration? (Hope you've got tests:)).
Related videos on Youtube
jdog
Updated on November 16, 2020Comments
-
jdog over 3 years
I need to change my column type from date to datetime for an app I am making. I don't care about the data as its still being developed.
How can I do this?
-
apneadiving about 13 yearsYou're right, I just assumed a beginner would choose the latest technology available, but that's, of course, unsure
-
davekaro over 11 yearsThe change method only works with reversible migrations. The code above would throw a ActiveRecord::IrreversibleMigration exception. Only methods in api.rubyonrails.org/classes/ActiveRecord/Migration/… should be used in the change method.
-
Sucrenoir about 11 yearsThe question is tagged "ruby-on-rails-3"
-
Jason about 11 years@Sucrenoir Yeah the tag was added by apneadiving after he answered.
-
geekQ about 10 yearsSyntax changed in Rails 4, s. response by Thomas Klemm
-
Dennis about 10 yearsIf you're wondering why a single
change
method isn't used instead of theup
anddown
methods, it's because thechange
method doesn't support thechange_column
migration definition. -
Jose B almost 10 yearsYou could potentially care about using a migration in a development environment, even if you don't care about the data, if you are working in a team and you want your schema change to propagate to all other developers in your team.
-
ap2 almost 10 yearsI'm having trouble seeing what advantage having the additional migration to change a column gives you in this situation. What is wrong with changing the original migration that created the column? In either case, each team member has to rerun all migrations to get the new schema.
-
Alan Peabody almost 10 yearsThis answer is only partially correct, you can not use change_column inside change even on rails 4 or down migration will not work. You should use up/down no matter the version of rails.
-
apneadiving almost 10 years@AlanPeabody rails doc on migrations states the contrary, do you have something to back what you're saying?
-
Alan Peabody almost 10 years@apneadiving change_column may work in some limited scenarios, but generally it is not really safe to use it in change. See: guides.rubyonrails.org/migrations.html#using-the-change-method and try a few different examples and you will see many break.
-
harryt over 9 yearsI am running Rails 4 and did this kind of migration before. CHANGE DOES NOT WORK! @davekaro's comment is correct.
-
jazzpi over 8 yearsIf you use a new migration, you can just undo the migration that changed the column type. If you were to edit the original, you would have to rollback that edit and rerun the migrations after that.
-
Ryan McGeary about 8 yearsThis is actually a very prudent answer considering there's no production data yet. For those worrying about other team members, that's what
rake db:migrate:reset
is for. -
W.M. over 7 yearsFor Rails 5, this is the correct and working solution.
-
BenKoshy over 7 yearsdoes this preserve the original data?
-
oligan about 7 yearsWhen being reversed, how would it know what the old column type it should change back to is?
-
Marklar over 6 years@AndrewGrimm you are correct. This is what I see when I try to reverse my migration:
This migration uses change_column, which is not automatically reversible.
To make the migration reversible you can either:
1. Define #up and #down methods in place of the #change method.
2. Use the #reversible method to define reversible behavior.
-
Mauro almost 5 yearsYes, preserve the original data