How to drop columns using Rails migration

前端 未结 20 753
执笔经年
执笔经年 2020-12-12 08:50

What\'s the syntax for dropping a database table column through a Rails migration?

相关标签:
20条回答
  • 2020-12-12 09:37

    Give below command it will add in migration file on its own

    rails g migration RemoveColumnFromModel
    

    After running above command you can check migration file remove_column code must be added there on its own

    Then migrate the db

    rake db:migrate
    
    0 讨论(0)
  • 2020-12-12 09:37

    first try to create a migration file running the command:

    rails g migration RemoveAgeFromUsers age:string
    

    and then on the root directory of the project run the migration running the command:

    rails db:migrate
    
    0 讨论(0)
  • 2020-12-12 09:39

    Rails 4 has been updated, so the change method can be used in the migration to drop a column and the migration will successfully rollback. Please read the following warning for Rails 3 applications:

    Rails 3 Warning

    Please note that when you use this command:

    rails generate migration RemoveFieldNameFromTableName field_name:datatype
    

    The generated migration will look something like this:

      def up
        remove_column :table_name, :field_name
      end
    
      def down
        add_column :table_name, :field_name, :datatype
      end
    

    Make sure to not use the change method when removing columns from a database table (example of what you don't want in the migration file in Rails 3 apps):

      def change
        remove_column :table_name, :field_name
      end
    

    The change method in Rails 3 is not smart when it comes to remove_column, so you will not be able to rollback this migration.

    0 讨论(0)
  • 2020-12-12 09:44

    You can try the following:

    remove_column :table_name, :column_name
    

    (Official documentation)

    0 讨论(0)
  • 2020-12-12 09:45

    Clear & Simple Instructions for Rails 5 & 6

    • WARNING: You will lose data if you remove a column from your database. To proceed, see below:
    • Warning: the below instructions are for trivial migrations. For complex migrations with e.g. millions and millions of rows, you will have to account for the possibility of failures, you will also have to think about how to optimise your migrations so that they run swiftly, and the possibility that users will use your app while the migration process is occurring. If you have multiple databases, or if anything is remotely complicated, then don't blame me if anything goes wrong!

    1. Create a migration

    Run the following command in your terminal:

    rails generate migration remove_fieldname_from_tablename fieldname:fieldtype

    Note: the table name should be in plural form as per rails convention.

    Example:

    In my case I want to remove the accepted column (a boolean value) from the quotes table:

    rails g migration RemoveAcceptedFromQuotes accepted:boolean

    See the documentation re: a convention when adding/removing fields to a table:

    There is a special syntactic shortcut to generate migrations that add fields to a table.

    rails generate migration add_fieldname_to_tablename fieldname:fieldtype

    2. Check the migration

    # db/migrate/20190122035000_remove_accepted_from_quotes.rb
    class RemoveAcceptedFromQuotes < ActiveRecord::Migration[5.2]
      # with rails 5.2 you don't need to add a separate "up" and "down" method.
      def change
        remove_column :quotes, :accepted, :boolean
      end
    end
    

    3. Run the migration

    rake db:migrate or rails db:migrate (they're both the same)

    ....And then you're off to the races!

    0 讨论(0)
  • 2020-12-12 09:46
    rails g migration RemoveXColumnFromY column_name:data_type
    

    X = column name
    Y = table name

    EDIT

    Changed RemoveXColumnToY to RemoveXColumnFromY as per comments - provides more clarity for what the migration is actually doing.

    0 讨论(0)
提交回复
热议问题