Ruby-on-rails 中的迁移问题
大家好,当我第一次开始 Rails 项目时,设计并创建了模型用户。完成所有迁移部分后,它在 postgres 成功创建了表“users”。 好吧,在项目期间做了一些更改后,我意识到表中缺少一个属性/新列。
所以我所做的就是从 postgres 中删除表 users 并在我的第一个迁移 ruby 类中添加一个新列:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.string :password
t.string :email
t.string :authorization_token //this is the new attribute that I insert
t.datetime :created_at
t.datetime :updated_at
t.timestamps
end
end
def self.down
drop_table :users
end
end
因此,当我再次运行 db:migrate 时,将使用新属性 :authorization_token 创建一个新的用户表,它不起作用,但没有错误。
(我知道我不应该删除桌子,还有另一种聪明的方法可以做到这一点)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Rails 的技巧——不要使用 SQL 手动修改表。当您看到问题时,您应该编写一个新的迁移,如 @nruth 所示。运行 rake:migrate 命令对您来说效果很好。
在这种情况下,由于您已经删除了“users”表,因此您现在遇到的问题是您的数据库架构与 Rails 认为的不同步。要解决此问题,您可以通过手动创建“用户”表,运行向下迁移,然后运行向上迁移,使数据库模式大致匹配 Rails 认为的内容。或者,您可以让 Rails 了解“users”表不再存在这一事实。 Rails 将迁移信息存储在 schema_info 表 (Rails < 2.1) 或 schema_migrations 表 (Rails >= 2.1) 中。如果您删除该表,Rails 会认为该架构不存在,并尝试运行所有向上迁移并再次为您重新创建“users”表。
最后,随着时间的推移,您可能会积累大量迁移,这些迁移会单独添加您忘记包含的一两列。如果您尚未发货或尚未投入生产,那么您可以编写一个迁移来为您的表建立基线。它看起来像这样:
这将强制删除表并使用您想要的所有属性重新创建它。
A tip for working with Rails -- do not hand modify your tables using SQL. When you saw the problem you should have written a new migration like @nruth showed. Running the rake:migrate command would have worked fine for you.
In this case since you've already deleted your 'users' table you now have the problem that your database schema is out of sync with what Rails thinks it is. To fix this problem you can either get the database schema to roughly match what Rails thinks it is by hand creating the 'users' table, running the down migration and then then the up migration. Or you can get Rails up to speed with the fact that the 'users' table no longer exists. Rails stores migration info in either a schema_info table (Rails < 2.1) or schema_migrations table (Rails >= 2.1). If you remove that table then Rails will think the schema does not exist and try to run all the up migrations and recreate the 'users' table for you again.
Lastly, over time you may accumulate a number of migrations that individually add a column or two that you forgot to include. If you haven't yet shipped or aren't in production yet, then you can write a migration that sort of baselines your table. It would look something like this:
This will forcibly drop the table and recreate it with all the attributes that you want.
迁移运行一次&已使用时存储在数据库中(查看 schema_migrations 表)。您可以尝试使用 rake db:migrate:reset 重新运行初始迁移,但最好只添加新的迁移(当数据库中有数据时,您不会想破坏数据库),如下所示:
script/generate迁移 add_authorization_token_to_usersauthorization_token:string
将生成类似于以下内容的内容:
要了解添加/删除列、change_table 等的工作原理,请查看 http://api.rubyonrails.org 或 http://guides.rubyonrails.org/migrations.html
Migrations are run once & stored in the database as having been used (take a look in the schema_migrations table). You could try using rake db:migrate:reset to re-run your initial migration, but it's better to just add new migrations (you won't want to blow away your database when it has data in it) as follows:
script/generate migration add_authorization_token_to_users authorization_token:string
which will generate something similar to the following:
To see how add/remove column, change_table, etc work, take a look at ActiveRecord::ConnectionAdapters::SchemaStatements at http://api.rubyonrails.org or http://guides.rubyonrails.org/migrations.html