Adding a column to an existing table in a Rails migration

后端 未结 11 1768
借酒劲吻你
借酒劲吻你 2020-11-28 00:34

I have a Users model which needs an :email column (I forgot to add that column during the initial scaffold).

I opened the migration file and added

相关标签:
11条回答
  • 2020-11-28 00:35

    You can also add column to a specific position using before column or after column like:

    rails generate migration add_dob_to_customer dob:date
    

    The migration file will generate the following code except after: :email. you need to add after: :email or before: :email

    class AddDobToCustomer < ActiveRecord::Migration[5.2]
      def change
        add_column :customers, :dob, :date, after: :email
      end
    end
    
    0 讨论(0)
  • 2020-11-28 00:38

    Sometimes rails generate migration add_email_to_users email:string produces a migration like this

    class AddEmailToUsers < ActiveRecord::Migration[5.0]
      def change
      end
    end
    

    In that case you have to manually an add_column to change:

    class AddEmailToUsers < ActiveRecord::Migration[5.0]
      def change
        add_column :users, :email, :string
      end
    end
    

    And then run rake db:migrate

    0 讨论(0)
  • 2020-11-28 00:47

    To add a column I just had to follow these steps :

    1. rails generate migration add_fieldname_to_tablename fieldname:string

      Alternative

      rails generate migration addFieldnameToTablename

      Once the migration is generated, then edit the migration and define all the attributes you want that column added to have.

      Note: Table names in Rails are always plural (to match DB conventions). Example using one of the steps mentioned previously-

      rails generate migration addEmailToUsers

    2. rake db:migrate

    Or

    1. You can change the schema in from db/schema.rb, Add the columns you want in the SQL query.
    2. Run this command: rake db:schema:load

      Warning/Note

      Bear in mind that, running rake db:schema:load automatically wipes all data in your tables.

    0 讨论(0)
  • 2020-11-28 00:49

    If you have already run your original migration (before editing it), then you need to generate a new migration (rails generate migration add_email_to_users email:string will do the trick). It will create a migration file containing line: add_column :users, email, string Then do a rake db:migrate and it'll run the new migration, creating the new column.

    If you have not yet run the original migration you can just edit it, like you're trying to do. Your migration code is almost perfect: you just need to remove the add_column line completely (that code is trying to add a column to a table, before the table has been created, and your table creation code has already been updated to include a t.string :email anyway).

    0 讨论(0)
  • 2020-11-28 00:49

    You can also do this .. rails g migration add_column_to_users email:string

    then rake db:migrate also add :email attribute in your user controller ;

    for more detail check out http://guides.rubyonrails.org/active_record_migrations.html

    0 讨论(0)
  • 2020-11-28 00:53

    You also can use special change_table method in the migration for adding new columns:

    change_table(:users) do |t|
      t.column :email, :string
    end
    
    0 讨论(0)
提交回复
热议问题