Obromios
Obromios

Reputation: 16383

How to update a column of type array with postgresql and ruby on rails

I have a column for a table that was created with following ruby on rails migration

def change    
    add_column :matches, :st_history, :smallint, array: true, default: []
end

on a postresql database. I wish to reset all values of the column back to default. I have tried

Match.update_all(st_history: [])

but this does not change any of the fields. Looking at the api documentation, it states that 'It should receive only values that can be passed as-is to the SQL database', so I suspect that the array is a complex datatype that will not work with a simple update_all command. The database has many millions of rows, so I do not want to update each row individually. What is a fast way of doing this?

Upvotes: 1

Views: 1961

Answers (2)

Aniket Tiwari
Aniket Tiwari

Reputation: 3998

You can try by creating a migration .And can give a default value

update_column :table_name, :col_name, :integer, array: true, null: false, default '{}'

Upvotes: 1

EJAg
EJAg

Reputation: 3298

This actually works. Just need to reload.

Upvotes: 1

Related Questions