2016-07-07 114 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,這次沒有問題出現。

僅供參考。希望這可以幫助那裏的人。