2016-07-07 143 views
0

,这里是我当前的用户模式:问题与安装设计

# == 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 ...这是什么来up:

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 
+0

可能你的方案已经过时了,但是在你的数据库的表中如果有一个字段。您可以重命名字段电子邮件迁移'db/migrate/2016xxxxxx_devise_create_users.rb'并再次运行('bundle exec rake db:migrate')。 – kalelc

+0

@kalec,我的模式不过时。它根本没有电子邮件列。我知道。 =) – user273072545345

+0

@kalelc,我原来的用户架构没有它......但我只是用设计迁移文件更新了我的帖子,我无法运行......因此它的错误......它列出了电子邮件字段在那...但为什么它尖叫重复让我困惑​​... – user273072545345

回答

0

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

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

请注意,要使t.change正常工作,您必须指定要更改字段的类型。在上述电子邮件迁移的情况下,电子邮件字段是字符串类型。

你可以参考在:devise wiki

+0

我宁愿你评论你已经有的所有列,但由设计重新生成。如果您需要更改数据类型,那么当您运行此行'rails generate devise user'时,可以在另一个迁移 – oreoluwa

+0

@ user27307254534534534543675765中执行此操作,迁移文件已创建,并且它被称为'DeviseCreateUser'。你可以在'db/migrate'文件夹中检查这个文件 –

+0

@Khanh Pham,道歉,我刚刚用devise的迁移文件更新了我的问题......正如你所看到的,它包含你建议运行的email列。 .. – user273072545345

0

打开了所有我所要做的就是做rake db:setup它再现了分贝。然后我跑了rake db:migrate,这次没有问题出现。

仅供参考。希望这可以帮助那里的人。