安装设备

时间:2016-07-07 23:07:28

标签: ruby-on-rails ruby-on-rails-4 devise

这是我当前的用户架构:

# == Schema Information
#
# Table name: users
#
#  id         :integer          not null, primary key
#  first_name :string
#  last_name  :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#
require 'elasticsearch/model'

class User < ActiveRecord::Base
    searchkick word_start: [:user]
    has_many :posts
    validates :first_name, :last_name, presence: true
end

这些是我能够在问题出现之前经历的步骤:

rails generate devise:install     
rails generate devise user        
rake db:migrate  

一旦我尝试迁移它,就会出现这种情况:

== 20160707230510 AddDeviseToUsers: migrating =================================
-- change_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateColumn: ERROR:  column "email" of relation "users" already exists
: ALTER TABLE "users" ADD "email" character varying DEFAULT '' NOT NULL
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:5:in `block in up'
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:3:in `up'
ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR:  column "email" of relation "users" already exists
: ALTER TABLE "users" ADD "email" character varying DEFAULT '' NOT NULL
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:5:in `block in up'
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:3:in `up'
PG::DuplicateColumn: ERROR:  column "email" of relation "users" already exists
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:5:in `block in up'
/Users/aa/Documents/railsy/basicProject/db/migrate/20160707230510_add_devise_to_users.rb:3:in `up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

从我之前的用户架构中可以看出,那里没有电子邮件列。所以...为什么会出现这个错误?

**编辑 - 应该发布设计迁移文件 - 不起作用的文件**

class AddDeviseToUsers < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.inet     :current_sign_in_ip
      t.inet     :last_sign_in_ip


      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      # Uncomment below if timestamps were not included in your original model.
      # t.timestamps null: false
    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
  end

  def self.down
    # By default, we don't want to make any assumption about how to roll back a migration when your
    # model already existed. Please edit below which fields you would like to remove in this migration.
    raise ActiveRecord::IrreversibleMigration
  end
end

再次编辑

当我运行rails g devise user时...就会出现这种情况:

Running via Spring preloader in process 72318
      invoke  active_record
      create    db/migrate/20160707230510_add_devise_to_users.rb
      insert    app/models/user.rb
       route  devise_for :users

2 个答案:

答案 0 :(得分:1)

原来我要做的就是做rake db:setup重新创建数据库。然后我跑了rake db:migrate,这次没有出现任何问题。

FYI。希望这可以帮助那些人。

答案 1 :(得分:0)

要消除重复字段上的迁移错误,请使用t.change,如下所示。

t.change :email, :string,     :null => false, :default => "" 

请注意,要使t.change生效,您必须指定要更改的字段的类型。对于上面的电子邮件迁移,电子邮件字段的类型为字符串。

您可以参考:devise wiki