Search code examples
rubymigrationsequel

Sequel Migration update with a row's ID


How can you run a Sequel migration that updates a newly added column with a value from the row?

The Sequel documentation shows how you can update the column with a static value:

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

What I need to do is update the new column with the value of the ID column:

something like:

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

But the above doesn't work and I have been unable to figure out how to get it to go.

Thanks!


Solution

  • artist in your loop is a Hash, so you are calling Hash#update, which just updates the Hash instance, it doesn't modify the database. That's why your loop doesn't appear to do anything.

    I could explain how to make the loop work (using all instead of each and updating a dataset restricted to the matching primary key value), but since you are just assigning the value of one column to the value of another column for all rows, you can just do:

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