0

我正在使用Devise遷移文件。原來是這樣的:更新了rails3遷移,但結果不顯示後耙:db遷移

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 

我想補充這樣的3列:

t.first_name t.last_name t.organization_name

所以我遷移文件看起來像這樣一次我所做的更改:

class DeviseCreateUsers < ActiveRecord::Migration 
    def self.up 
    create_table(:users) do |t| 
     t.database_authenticatable :null => false 
     t.recoverable 
     t.rememberable 
     t.trackable 
     t.first_name 
     t.last_name 
     t.organization_name 
     # 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 

然後在命令行中我跑這個命令:

rake db:migrate 

,並將所得db表沒有反映出我嘗試添加列。下面是它的樣子:

describe users; 
+------------------------+--------------+------+-----+---------+----------------+ 
| Field     | Type   | Null | Key | Default | Extra   | 
+------------------------+--------------+------+-----+---------+----------------+ 
| id      | int(11)  | NO | PRI | NULL | auto_increment | 
| email     | varchar(255) | NO | UNI |   |    | 
| encrypted_password  | varchar(128) | NO |  |   |    | 
| reset_password_token | varchar(255) | YES | UNI | NULL |    | 
| reset_password_sent_at | datetime  | YES |  | NULL |    | 
| remember_created_at | datetime  | YES |  | NULL |    | 
| sign_in_count   | int(11)  | YES |  | 0  |    | 
| current_sign_in_at  | datetime  | YES |  | NULL |    | 
| last_sign_in_at  | datetime  | YES |  | NULL |    | 
| current_sign_in_ip  | varchar(255) | YES |  | NULL |    | 
| last_sign_in_ip  | varchar(255) | YES |  | NULL |    | 
| created_at    | datetime  | YES |  | NULL |    | 
| updated_at    | datetime  | YES |  | NULL |    | 
+------------------------+--------------+------+-----+---------+----------------+ 

任何想法,爲什麼我試圖更改不顯示出來?我如何強制更改發生?

謝謝!

回答

1

最好不要改變現有的移民,即使是在發展,但有時它是可以接受的。

如果您已經運行此遷移,就不會再使用rake db:migrate,因爲只有新的遷移運行比架構的版本將運行。

要再次運行上次遷移,你可以做rake db:migrate:redo

+0

做一個重做給了這個錯誤:耙子中止! 發生錯誤,所有後來的遷移取消: 未定義的方法'first_name'爲# GeekedOut 2011-05-20 22:33:42

+0

我還將這個:first_name,:last_name,:organization_name添加到我的users.rb文件,但它似乎沒有幫助。 – GeekedOut 2011-05-20 22:46:24

+0

看起來您最好將所有更改保存到遷移中,手動將其遷移下來:rake db:migrate:down VERSION = nnnnnnnnnnnn',重新應用您的更改並按正常方式執行遷移。改變遷移通常不是一個好主意,最好的做法是修改一個新的數據庫。 – 2011-05-20 23:00:17

3

解決您的遷移文件,它有一些錯誤:

... 
t.first_name 
t.last_name 
t.organization_name 
... 

其更改如下:

... 
t.string :first_name 
t.string :last_name 
t.string :organization_name 
... 

您可以檢查Migration guide瞭解更多信息。

在這之後的變化,如果表users不存在,你可以做rake db:migrate;如果存在的話。

順便說一句是更好地使用其他遷移添加/刪除你的表/變更欄。