我正在使用 Devise 迁移文件。原来是这样的:
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.姓氏 t.组织名称
因此,在我进行更改后,我的迁移文件看起来像这样:
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
生成的数据库表没有反射(reflect)我尝试添加的列。这是它的样子:
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 | |
+------------------------+--------------+------+-----+---------+----------------+
知道为什么我尝试的更改没有显示出来吗?我如何强制更改发生?
谢谢!!
最佳答案
修复你的迁移文件,它有一些错误:
...
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
;如果存在,请执行 rake db:migrate:redo
。
顺便说一句,您最好使用另一个迁移来添加/删除/更改表上的列。
关于mysql - 更新了 rails 3 迁移,但结果在 take :db migrate 后不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6078168/