Rails migration for change column

后端 未结 9 988
一个人的身影
一个人的身影 2020-11-28 17:24

We have script/generate migration add_fieldname_to_tablename fieldname:datatype syntax for adding new columns to a model.

On the same line, do we have a

相关标签:
9条回答
  • 2020-11-28 18:01

    Just generate migration:

    rails g migration change_column_to_new_from_table_name
    

    Update migration like this:

    class ClassName < ActiveRecord::Migration
      change_table :table_name do |table|
        table.change :column_name, :data_type
      end
    end
    

    and finally

    rake db:migrate
    
    0 讨论(0)
  • 2020-11-28 18:06

    I think this should work.

    change_column :table_name, :column_name, :date
    
    0 讨论(0)
  • 2020-11-28 18:09

    Another way to change data type using migration

    step1: You need to remove the faulted data type field name using migration

    ex:

    rails g migration RemoveFieldNameFromTableName field_name:data_type
    

    Here don't forget to specify data type for your field

    Step 2: Now you can add field with correct data type

    ex:

    rails g migration AddFieldNameToTableName field_name:data_type
    

    That's it, now your table will added with correct data type field, Happy ruby coding!!

    0 讨论(0)
  • 2020-11-28 18:10

    This is all assuming that the datatype of the column has an implicit conversion for any existing data. I've run into several situations where the existing data, let's say a String can be implicitly converted into the new datatype, let's say Date.

    In this situation, it's helpful to know you can create migrations with data conversions. Personally, I like putting these in my model file, and then removing them after all database schemas have been migrated and are stable.

    /app/models/table.rb
      ...
      def string_to_date
        update(new_date_field: date_field.to_date)
      end
    
      def date_to_string
        update(old_date_field: date_field.to_s)
      end
      ...
    
        def up
            # Add column to store converted data
            add_column :table_name, :new_date_field, :date
            # Update the all resources
            Table.all.each(&:string_to_date)
            # Remove old column
            remove_column :table_name, :date_field
            # Rename new column
            rename_column :table_name, :new_date_field, :date_field
        end
    
        # Reversed steps does allow for migration rollback
        def down
            add_column :table_name, :old_date_field, :string
            Table.all.each(&:date_to_string)
            remove_column :table_name, :date_field
            rename_column :table_name, :old_date_field, :date_field
        end
    
    0 讨论(0)
  • 2020-11-28 18:12

    I'm not aware if you can create a migration from the command line to do all this, but you can create a new migration, then edit the migration to perform this taks.

    If tablename is the name of your table, fieldname is the name of your field and you want to change from a datetime to date, you can write a migration to do this.

    You can create a new migration with:

    rails g migration change_data_type_for_fieldname
    

    Then edit the migration to use change_table:

    class ChangeDataTypeForFieldname < ActiveRecord::Migration
      def self.up
        change_table :tablename do |t|
          t.change :fieldname, :date
        end
      end
      def self.down
        change_table :tablename do |t|
          t.change :fieldname, :datetime
        end
      end
    end
    

    Then run the migration:

    rake db:migrate
    
    0 讨论(0)
  • 2020-11-28 18:12

    With Rails 5

    From Rails Guides:

    If you wish for a migration to do something that Active Record doesn’t know how to reverse, you can use reversible:

    class ChangeTablenameFieldname < ActiveRecord::Migration[5.1]
      def change
        reversible do |dir|
          change_table :tablename do |t|
            dir.up   { t.change :fieldname, :date }
            dir.down { t.change :fieldname, :datetime }
          end
        end
      end
    end
    
    0 讨论(0)
提交回复
热议问题