【发布时间】:2012-07-31 15:35:34
【问题描述】:
我正在编写一个在 postgres 表中填充 premium_from datetime 列的 Rails 迁移。
我对人口没有问题,但我一直在为迁移编写回滚方法,因为当我尝试将 datetime 属性更改回nil 时,rails 或 postgres 似乎什么都不做。我也无法得到任何有用的错误消息,所以我完全不确定发生了什么。
这是我最近的迭代(出于绝望,我尝试单独处理帐户,因为似乎也没有其他方法):
def self.down
Account.where('premium_from is not null').each do |a|
a.update_attribute(:premium_from, 'null')
end
end
这愉快地运行了大约 7 秒,然后让我所有填充的 premium_from 条目仍然完好无损,我也无法在谷歌中找到任何有用的东西。
有人知道如何在 postgres 中将日期时间列条目设置为 nil 吗?
或者我只是忽略了一些明显的东西?
编辑:向上看起来像这样
csv.each do |row|
if account = Account.premium.find_by_name(row.first)
# This .find is necessary because .premium uses a join, meaning that it returns
# read-only objects
Account.find(account.id).update_attribute(:premium_from, Time.parse(row.last))
end
end
【问题讨论】:
-
已添加。如果您想知道的话,
premium_from列是在之前的迁移中添加的。
标签: ruby-on-rails postgresql rails-migrations