Errors while performing a user migration

I am trying to implement the Devise authentication library and also add columns that I may need to use that are particular to my own application.

I run the rake migration command and I get a strange error. Here is my devise_create_users file:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :confirmation_token,   :unique => true
    # add_index :users, :unlock_token,         :unique => true
    # add_index :users, :authentication_token, :unique => true
  end

  def self.down
    drop_table :users
  end
end

And a minimal create_users file

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

But the strange thing is that when I run the migration, I get this error:

Mysql2::Error: Table 'users' already exists: CREATE TABLE `users` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `email` varchar(255) DEFAULT '' NOT NULL, `encrypted_password` varchar(128) DEFAULT '' NOT NULL, `reset_password_token` varchar(255), `reset_password_sent_at` datetime, `remember_created_at` datetime, `sign_in_count` int(11) DEFAULT 0, `current_sign_in_at` datetime, `last_sign_in_at` datetime, `current_sign_in_ip` varchar(255), `last_sign_in_ip` varchar(255), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB

Which is very weird because I never mention any of these columns in the files listed above. Where are the extra columns coming from? And should my second create_users file be an update instead of a create?

Thanks!

Answers


You are having this problem because, you are trying to create table users twice.

Your first migration will create table users and the strange columns you see are created by devise.

If you need to update your columns use:

class CreateUsers < ActiveRecord::Migration
  def self.up
    add_column :table_name, :new_column_name, :data_type  #add new column
    remove_column :table_name, :column_to_remove          #remove an existing column
  end

  ...
end

Take a look at Migrations for more information.


Need Your Help

How can I deploy (create .ipa) iphone app using 'cordova build ios --release'?

cordova deployment ipa

I have created a helloworld ios app with cordova using there documentation. The app successfully runs when I run cordova emulate ios. What I can't do is while deploying when I run cordova build

Getting closest point on grid to point

c++ algorithm computational-geometry

I have a one dimensional gird. its spacing is a floating point. I have a point with floating point coordinate as well. I need to find its distance to the closest grid point.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.