Change a column type from Date to DateTime during ROR migration

165,818

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:)).

Share:
165,818

Related videos on Youtube

jdog
Author by

jdog

Updated on November 16, 2020

Comments

  • jdog
    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
    apneadiving about 13 years
    You're right, I just assumed a beginner would choose the latest technology available, but that's, of course, unsure
  • davekaro
    davekaro over 11 years
    The 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
    Sucrenoir about 11 years
    The question is tagged "ruby-on-rails-3"
  • Jason
    Jason about 11 years
    @Sucrenoir Yeah the tag was added by apneadiving after he answered.
  • geekQ
    geekQ about 10 years
    Syntax changed in Rails 4, s. response by Thomas Klemm
  • Dennis
    Dennis about 10 years
    If you're wondering why a single change method isn't used instead of the up and down methods, it's because the change method doesn't support the change_column migration definition.
  • Jose B
    Jose B almost 10 years
    You 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
    ap2 almost 10 years
    I'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
    Alan Peabody almost 10 years
    This 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
    apneadiving almost 10 years
    @AlanPeabody rails doc on migrations states the contrary, do you have something to back what you're saying?
  • Alan Peabody
    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-meth‌​od and try a few different examples and you will see many break.
  • harryt
    harryt over 9 years
    I am running Rails 4 and did this kind of migration before. CHANGE DOES NOT WORK! @davekaro's comment is correct.
  • jazzpi
    jazzpi over 8 years
    If 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
    Ryan McGeary about 8 years
    This 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.
    W.M. over 7 years
    For Rails 5, this is the correct and working solution.
  • BenKoshy
    BenKoshy over 7 years
    does this preserve the original data?
  • oligan
    oligan about 7 years
    When being reversed, how would it know what the old column type it should change back to is?
  • Marklar
    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
    Mauro almost 5 years
    Yes, preserve the original data