简体   繁体   English

如何在Rails上使用PostgreSQL和Ruby更新数组类型的列

[英]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 我有一个表的列,该表是在rails迁移中使用以下Ruby创建的

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

on a postresql database. 在postresql数据库上。 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. 查看api文档 ,它指出“它应该只接收可以原样传递给SQL数据库的值”,因此我怀疑该数组是复杂的数据类型,无法通过简单的update_all命令使用。 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? 快速的方法是什么?

您可以尝试创建迁移。并可以提供默认值

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

This actually works. 这实际上有效。 Just need to reload. 只需要重新加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查询Ruby on Rails和PostgreSQL上的数组类型字段 - Query for array type field on Ruby on Rails and PostgreSQL 在 Rails 上的 Ruby 中清空 PostgreSQL 文本数组列 - Emptying a PostgreSQL text array column in Ruby on Rails Rails 4 / Postgresql 9.4-查询和更新数组中的ruby对象 - Rails 4 / postgresql 9.4 - Query and update a ruby object inside an array 如何将PostgreSQL数组反序列化为ruby集而不是rails中的数组 - how to unserialize postgresql array as a ruby Set instead of an Array in rails 如何在 Rails 上更新 Ruby 中数组的第一个元素? - How to update the first element of an array in Ruby on Rails? Rails 4使用数组数据类型错误查询postgresql列 - Rails 4 querying against postgresql column with array data type error ruby / rails如何将列插入多维数组 - ruby/rails how to insert column into multidimensional array 如何在Ruby on Rails中使用PostgreSQL JSON数组查询ActiveRecord :: Relation - How to use the PostgreSQL JSON array query ActiveRecord::Relation in Ruby on Rails 如何在 Rails 6 上的 Ruby 上正确查询 Postgresql JSONB 哈希数组? - How to properly query Postgresql JSONB array of hashes on Ruby on Rails 6? ruby on rails postgresql 活动记录并行更新 - ruby on rails postgresql active record parallle update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM