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;如果存在的话。

顺便说一句是更好地使用其他迁移添加/删除你的表/变更栏。