Greg Olsen
Greg Olsen

Reputation: 912

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!

Upvotes: 0

Views: 1015

Answers (2)

stbnrivas
stbnrivas

Reputation: 643

if you need update all rows of a table, because it is a new column that need be populate

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

or if you need, update only a unique row into your migration file you can:

artists = DB[:artists]
artists.where(:id => 1).update(:column_name1 => 'new value1', :column_name2 => "other")

Upvotes: 0

Jeremy Evans
Jeremy Evans

Reputation: 12139

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)

Upvotes: 2

Related Questions