Skip to content

How To: Change an already existing table to add devise required columns

allysonbarros edited this page Jan 21, 2013 · 8 revisions

The following migration can be used to add devise required columns to an existing users table.

class AddDeviseColumnsToUser < ActiveRecord::Migration
  def change
    change_table :users do |t|
      # if you already have a email column, you have to comment the below line and add the :encrypted_password column manually (see devise/schema.rb).
      t.database_authenticatable
      t.confirmable
      t.recoverable
      t.rememberable
      t.trackable
      t.token_authenticatable
      t.timestamps
    end
  end
end

Note: If your existing users table already has a column email, then Devise's t.database_authenticatable will fail, because it tries to add email again. t.database_authenticatable will add two columns, email and encrypted_password. If email already exists, then just manually add the encrypted_password, in line of t.database_authenticatable:

  t.string :encrypted_password, :null => false, :default => '', :limit => 128

Devise 2.0 does not provide the migrations helpers, so you can see the list of columns generated automatically by Devise here:

https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style

Note: it is sometimes easier to just delete the table and re-create it using Devise instead of trying to retrofit Devise into your table.

Clone this wiki locally