2010-11-14 84 views
9

我正在從Authlogic遷移到Devise。在現有模型上設計遷移

更新:

色器件的遷移嘗試重新創建表的用戶,所以我改變了,你可以看到下面的CREATE_TABLE到change_table,並在年底下降表中刪除我添加

問題是當我運行耙子時出現錯誤。

這是運行耙機時得到的錯誤。

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

SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar(255) DEFAULT '' NOT NULL 

這是遷移

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

     # 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 
    end 

    def self.down 
    remove_column :users, :database_authenticatable 
    remove_column :users, :recoverable 
    remove_column :users, :rememberable 
    remove_column :users, :trackable 
    remove_index :users, :email 
    remove_index :users, :reset_password_token 
    end 
end 

在我schema.rb我已經從Authlogic有這個。

create_table "users", :force => true do |t| 
    t.string "username" 
    t.string "email" 
    t.string "crypted_password" 
    t.string "password_salt" 
    t.string "persistence_token" 

我認爲它看到某種衝突的,我是不能夠認識到如何避免與這些設計助手

謝謝!

+0

請將jamuraa的答案標爲正確。否則,這個問題出現在「未回答的問題列表」中。 – kikito 2012-03-30 18:57:16

回答

22

您得到的錯誤是因爲您正在嘗試重新創建您已有的email字段。 email字段在設計助手t.database_authenticatable中創建。您可以將舊用戶表與新系統一起使用,但不需要包含t.database_authenticatable,只需重命名舊字段名稱。查看Documentation for Devise,可以看到database_authenticatable只創建了三個字段:email,encrypted_pa​​ssword和password_salt。他們是一樣的電子郵件,crypted_pa​​ssword和password_salt你已經在你的authlogic用戶表,所以你可以使用change_table像這樣:

def self.up 
    change_table(:users) do |t| 
    t.recoverable 
    t.trackable 
    # rememberable uses remember_token, but this field is different 
    t.rename :remember_token_expires_at, :remember_created_at 
    # these fields are named differently in devise 
    t.rename :crypted_password, :encrypted_password 
    end 
end 
+0

謝謝你的回覆。已經解決了這個問題! – Martin 2010-11-27 12:04:01

+3

您可以回覆或更新原始問題與您的解決方案爲他人? – gallamine 2011-08-23 13:25:42

0
add_column(:users, :database_authenticatable, {:null=>false}) 

您不提供如何設置此字段的信息。它需要是一個字符串,我認爲你想傳遞這些信息。

add_column :users, :database_authenticatable, :string, :null=>false 

你所得到的是錯誤的詳細信息:

undefined method `to_sym' 

該錯誤的遷移是非常令人沮喪,因爲它缺少的內容,但它的意思是,你以錯誤的順序得到了遷移。

例如 - 如果你添加一個布爾文件的使用模式:

add_column :users, :user_is_a_jackass, :boolean, :default => false 

這將遷移確定。但是,如果你這樣做:

add_column :users, :user_is_a_jackass, :default => false 

你會得到同樣的錯誤,因爲你沒有告訴遷移它應該是什麼字段類型。

+0

謝謝你的回答。你說的是真實的,但我真的認爲最初的遷移使用設計助手,這就是爲什麼遷移避免定義類型。 我realice我可以改變create_table change_table,我只是更新問題。 – Martin 2010-11-14 03:19:42

4

,而不是僅僅create_table之前改變create_tablechange_table,你可以添加以下行:

rename_table :users, :old_users_authlogic 

然後,create_table之後:與

say_with_time 'Migrating users from Authlogic to Devise...' do 
    execute "INSERT INTO users (id, email, ...) SELECT id, email FROM old_users_authlogic" 
end 

如果您正在使用的索引參照完整性,請不要忘記將它們更新到新表中,因爲rename_table將使它們指向old_users_authlogic

0

我解決了這個刪除制定遷移文件,但不刪除它昇華。然後,我做了一個軌道遷移remove_email_from_foo,從而刪除了電子郵件屬性。然後我取消了Devise刪除遷移文件,該文件在&後面購買了遷移文件,運行rake db:migrate。設計保存至模型的屬性