Ruby-on-rails 中的迁移问题

发布于 2024-09-16 04:20:37 字数 702 浏览 6 评论 0 原文

大家好,当我第一次开始 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 创建一个新的用户表,它不起作用,但没有错误。

(我知道我不应该删除桌子,还有另一种聪明的方法可以做到这一点)

Hey guys, when I first begin a rails project, the model user was designed and created. After all the migration part, it successful created the table "users" at postgres.
Well, then after doing some changes during the project, I realized that was missing an attribute/new column at the table.

So what I did was delete the table users from postgres and add a new column at my first migration ruby class:

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

So, when I run again the db:migrate hopping that a new user table will be created with the new attribute :authorization_token, it doesn't work, but with no errors.

(I know that I should not remove the table, there is another smart way to do it)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

影子是时光的心 2024-09-23 04:20:37

使用 Rails 的技巧——不要使用 SQL 手动修改表。当您看到问题时,您应该编写一个新的迁移,如 @nruth 所示。运行 rake:migrate 命令对您来说效果很好。

在这种情况下,由于您已经删除了“users”表,因此您现在遇到的问题是您的数据库架构与 Rails 认为的不同步。要解决此问题,您可以通过手动创建“用户”表,运行向下迁移,然后运行向上迁移,使数据库模式大致匹配 Rails 认为的内容。或者,您可以让 Rails 了解“users”表不再存在这一事实。 Rails 将迁移信息存储在 schema_info 表 (Rails < 2.1) 或 schema_migrations 表 (Rails >= 2.1) 中。如果您删除该表,Rails 会认为该架构不存在,并尝试运行所有向上迁移并再次为您重新创建“users”表。

最后,随着时间的推移,您可能会积累大量迁移,这些迁移会单独添加您忘记包含的一两列。如果您尚未发货或尚未投入生产,那么您可以编写一个迁移来为您的表建立基线。它看起来像这样:

class CreateBaselineUsers < ActiveRecord::Migration
  def self.up
    create_table :users, :force => true do |t|
      t.string :name
      ...

这将强制删除表并使用您想要的所有属性重新创建它。

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:

class CreateBaselineUsers < ActiveRecord::Migration
  def self.up
    create_table :users, :force => true do |t|
      t.string :name
      ...

This will forcibly drop the table and recreate it with all the attributes that you want.

穿越时光隧道 2024-09-23 04:20:37

迁移运行一次&已使用时存储在数据库中(查看 schema_migrations 表)。您可以尝试使用 rake db:migrate:reset 重新运行初始迁移,但最好只添加新的迁移(当数据库中有数据时,您不会想破坏数据库),如下所示:

script/generate迁移 add_authorization_token_to_usersauthorization_token:string

将生成类似于以下内容的内容:

class AddAuthorizationTokenToUsers < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.string :authorization_token //this is the new attribute that I insert
    end
  end

  def self.down
    remove_column :users, :authorization_token
  end
end

要了解添加/删除列、change_table 等的工作原理,请查看 http://api.rubyonrails.orghttp://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:

class AddAuthorizationTokenToUsers < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.string :authorization_token //this is the new attribute that I insert
    end
  end

  def self.down
    remove_column :users, :authorization_token
  end
end

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文