0

如何运行 Sequel 迁移,使用行中的值更新新添加的列?

Sequel 文档显示了如何使用静态值更新列:

self[:artists].update(:location=>'Sacramento')

我需要做的是使用 ID 列的值更新新列:

就像是:

self[:artists].each |artist| do
  artist.update(:location => artist[:id])
end

但上述方法不起作用,我一直无法弄清楚如何让它去。

谢谢!

4

2 回答 2

2

artist在你的循环中是 a Hash,所以你正在调用Hash#update,它只是更新Hash实例,它不会修改数据库。这就是为什么您的循环似乎没有做任何事情的原因。

我可以解释如何使循环工作(使用all而不是each更新限制为匹配主键值的数据集),但是由于您只是将一列的值分配给所有行的另一列的值,因此您可以做:

self[:artists].update(:location=>:id)
于 2012-11-14T19:50:00.537 回答
0

如果您需要更新表的所有行,因为它是需要填充的新列

artists = DB[:artists]
artists.update(:column_name => 'new value')

或者,如果需要,仅将唯一行更新到迁移文件中,您可以:

artists = DB[:artists]
artists.where(:id => 1).update(:column_name1 => 'new value1', :column_name2 => "other")
于 2015-07-07T22:35:09.330 回答