使用 Rails 和 Postgresql 进行迁移时未填充默认值

发布于 2024-10-20 05:53:34 字数 462 浏览 1 评论 0原文

我目前正在尝试运行此迁移:

class AddDroppedProjectsCountToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :dropped_projects, :integer, {:default=>0, :required=>true}
  end

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

该列已正确添加,但没有任何旧记录填充为 0。它们为零。我也尝试过使用 default=>'0' ,但没有成功。知道为什么会发生这种情况吗? (Rails 3.0.3)


编辑添加:当我创建一个新用户时,它工作正常,并且一切看起来都正确。只是老用户在表中该值仍然为零。

I am currently trying to run this migration:

class AddDroppedProjectsCountToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :dropped_projects, :integer, {:default=>0, :required=>true}
  end

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

The column is added correctly, but none of the old records are populated with 0. They are nil. I have tried using default=>'0' as well, to no avail. Any idea why this might be happening? (Rails 3.0.3)


Edited to add: When I create a new user it works fine, and everything looks correct. It's just the old users that still have nil for that value in the table.

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

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

发布评论

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

评论(3

海风掠过北极光 2024-10-27 05:53:34

如果你说:

def self.up
  add_column :users, :dropped_projects, :integer, :null => false, :default => 0
end

相反,会发生什么?如果没有 :null=>false,您仍然允许在 dropped_projects 中使用 NULL,因此 PostgreSQL 没有理由将它们设置为 0。另外,我不认为 :requiredadd_column;由于选项只是一个简单的 Hash 并且 add_column 仅查找它知道的选项,因此您的杂散 :required 选项将被默默地忽略。

What happens if you say:

def self.up
  add_column :users, :dropped_projects, :integer, :null => false, :default => 0
end

instead? Without the :null=>false you're still allowing NULL in dropped_projects so there's no reason for PostgreSQL to make them 0. Also, I don't think :required is a valid option for add_column; since the options are just a simple Hash and add_column only looks for options it knows about, your stray :required option is silently ignored.

红玫瑰 2024-10-27 05:53:34

你可以这样做:(

取自 http://apidock.com/rails/ActiveRecord/Migration

更改表后使用模型

有时您需要在迁移中添加一列并填充它
紧接着。在这种情况下,您需要拨打电话
Base#reset_column_information 以确保模型具有
添加新列后的最新列数据。示例:

class AddPeopleSalary < ActiveRecord::Migration
  def up
    add_column :people, :salary, :integer
    Person.reset_column_information
    Person.all.each do |p|
      p.update_column :salary, SalaryCalculator.compute(p)
    end
  end
end

you could do this:

(taken from http://apidock.com/rails/ActiveRecord/Migration)

Using a model after changing its table

Sometimes you’ll want to add a column in a migration and populate it
immediately after. In that case, you’ll need to make a call to
Base#reset_column_information in order to ensure that the model has
the latest column data from after the new column was added. Example:

class AddPeopleSalary < ActiveRecord::Migration
  def up
    add_column :people, :salary, :integer
    Person.reset_column_information
    Person.all.each do |p|
      p.update_column :salary, SalaryCalculator.compute(p)
    end
  end
end
迷离° 2024-10-27 05:53:34

我相信这是因为您正在更改旧的迁移,而不是创建新的迁移。
在这种情况下,解决方案是检查架构文件 (schema.rb)。它不会自动改变,并添加

t.integer "dropped_projects", default: 0, null: false

I believe this is due to the fact that you are changing the old migration, instead of creating a new.
In this case, the solution is to check the schema file (schema.rb). It does not change automatically, and add

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