Change a column type from Date to DateTime during ROR migration

后端 未结 5 945
名媛妹妹
名媛妹妹 2020-12-07 08:58

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?

5条回答
  •  萌比男神i
    2020-12-07 09:30

    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
    

提交回复
热议问题