Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Adds migration and type casting support for PostgreSQL Array datatype #7547
PostgreSQL array type support. Any datatype can be used to create an array column, with full migration and schema dumper support.
To declare an array column, use the following syntax:
create_table :table_with_arrays do |t| t.integer :int_array, :array => true # integer t.integer :int_array, :array => true, :length => 2 # smallint t.string :string_array, :array => true, :length => 30 # char varying(30) end
This respects any other migraion detail (limits, defaults, etc). ActiveRecord will serialize and deserialize the array columns on their way to and from the database.
One thing to note: PostgreSQL does not enforce any limits on the number of elements, and any array can be multi-dimensional. Any array that is multi-dimensional must be rectangular (each sub array must have the same number of elements as its siblings).
I can squash this commit after review
I did have a problem bootstrapping your fork:
gem 'rails', :git => 'git://github.com/danmcclain/rails.git', :branch => 'pg-arrays'
Then I added the
$ bundle exec rake db:create Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (pg_array_parser is not part of the bundle. Add it to Gemfile.) /Users/schneems/.rvm/gems/ruby-1.9.3-p194@heroku/gems/bundler-1.2.0/lib/bundler/rubygems_integration.rb:147:in `block in replace_gem'
After adding it to my Gemfile, everything worked like suspected and I was able to add an integer array column to an existing table:
def change add_column :foos, :int_array, :integer, :array => true end
It's possible I did something wrong in the setup, but otherwise - as it stands this won't work out of the box. Let me know if you have debugging steps. I would like to see maximum data type support for PG in Rails.
Just so everybody is on the same page, the
So that being said, there are two options here with trade offs:
Am I missing any other potential options?
@bcardarella thanks for the info, and @danmcclain thanks for the work... i'm good with any of those options. If we need integration in another library/gem, we'll likely need those accepted before this gets merged in. Having support for PG arrays in Rails shouldn't rely on any other libraries besides ActiveRecord and the 'pg' gem (or jdbc-postgres for jruby). If adding a library after the fact speeds things up, that would be fine, but it would be nice to get this functionality without having to add another gem to my Gemfile on new projects.
@rafaelfranca I think I have addressed all the issues you presented. I have also added a pure ruby implementation of
Verified that the latest changes don't rely on an external gem. I'm happy with the feature.