更新了 Rails3 迁移,但 rake:db migrate 后未显示结果
我正在使用 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.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
生成的数据库表没有反映我尝试添加的列。它是这样的:
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 | |
+------------------------+--------------+------+-----+---------+----------------+
知道为什么我尝试的更改没有显示出来吗?我如何强制改变发生?
谢谢!!
I am working with a Devise migration file. Originally it was like this:
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
I wanted to add 3 columns like this:
t.first_name
t.last_name
t.organization_name
So my migration file looked like this once I made the changes:
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
Then from command line I ran this command:
rake db:migrate
And the resulting db table did not reflect the columns I tried to add. Here is how it looks like:
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 | |
+------------------------+--------------+------+-----+---------+----------------+
Any idea why my attempted changes are not showing up? How do I force the changes to happen?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修复您的迁移文件,它有一些错误:
更改如下:
您可以查看迁移指南以获取更多信息。
更改后,如果表
users
不存在,您可以执行rake db:migrate
;如果存在,请rake db:migrate:redo
。顺便说一句,您最好使用另一个迁移来添加/删除/更改表上的列。
Fix your migration file, it has some errors:
Change it as follow:
You can check Migration guide for more information.
After this changes, if the table
users
doesn't exists, you can dorake db:migrate
; if it exists dorake db:migrate:redo
.Btw is better to you use another migration for add/remove/change columns on your tables.
最好不要更改现有的迁移,即使在开发中也是如此,但有时这是可以接受的。
如果您已经运行此迁移,则不会使用 rake db:migrate 再次运行它,因为只会运行比架构版本更新的迁移。
要再次运行上次迁移,您可以执行 rake db:migrate:redo
It is best not to change an existing migration, even in development, but sometime it is acceptable.
If you have already run this migration, it will not run again using
rake db:migrate
as only newer migrations than the schema version will be run.To run the last migration again you can do
rake db:migrate:redo