1

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?

3
  • I believe it works. Did you reload your models and check? Commented Sep 25, 2017 at 6:00
  • tell me whether the answer I provided was helpful or not Commented Sep 25, 2017 at 6:03
  • EJ2015, you were right, I just forgot to reload. Do you want to put this as an answer, I will accept it. It is too late to delete the question as someone has put an answer. Commented Sep 25, 2017 at 6:12

2 Answers 2

1

This actually works. Just need to reload.

Sign up to request clarification or add additional context in comments.

2 Comments

It turned out that although this works, but would take 80 hours to use on my database, so I ended up using Aniket's answer which only took a few seconds.
Just curious, how big is your db? update_all sends one SQL command to db, skipping ActiveRecord completely. Why would it take that long?
1

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 '{}'

1 Comment

Aniket, it should be 'change_column' not 'update_column'. For a large database this approach is magic. On my database it only took a few seconds instead of the 80 hours for my original approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.