开发者

Errors while performing a user migration

开发者 https://www.devze.com 2023-03-07 03:21 出处:网络
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 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 TABL开发者_C百科E `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!


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.


The first migration will create a table (create_table(:users)) with the name users and all the columns devise needs to operate. So your second migration is not necessary. It will fail because the table already exists. (also the timestamp fields are already in place) If you want to add other fields to the users table you could add them in the initial migration or make a migration to update the users table. Don't forget to add those fields as accessible attributes in your user model.

0

精彩评论

暂无评论...
验证码 换一张
取 消