45

In Rails it is possible to do:

add_index :table, :column_name, :using => 'btree'

Is it possible in Rails 4 with PGSQL to add a GIN or GiST index like:

add_index :students, :name, :using => 'gin'

Or do I have use manual execute statements? The notion behind this is I would like to keep schema.rb instead of using structure.sql

2 Answers 2

61

In Rails 4, you can now do something like this in a migration:

add_index :products, :data, using: :gin
Sign up to request clarification or add additional context in comments.

11 Comments

Is there any docs on this? I saw a similar answer on Stackoverflow, but add_index :products, :data, using: :gin raises an exception when used in a migration.
This raises an error PG::UndefinedObject: ERROR: data type character varying has no default operator class for access method "gin" ... and this is on Rails 4.2.
Small correction, you also need to give a configuration as attribute, e. g. 'english': execute("CREATE INDEX some_name ON products USING gin(to_tsvector('english', 'data'))")
@King'oriMaina In Rails 5 you can use the opclass argument to use a default operator class and avoid that error: add_index :products, :data, using: :gin, opclass: :gin_trgm_ops api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/…
Add enable_extension "btree_gin" in the migration above this step.
|
35

Wow! I had a few hairs turn gray on this one. I am using rails 4.2 and trying to run this migration and it was giving me the same error as people above.

PG::UndefinedObject: ERROR: data type character varying has no default

I found out that you can in fact still continue to use schema.rb and do not have to use the config/application.rb

config.active_record.schema_format = :sql

The one major thing I was missing was installing postgres contrib modules. I made the assumption that features like gin and gist were already turned on. I never noticed but modules are shown in your schema.rb file. They appear at the top like this

ActiveRecord::Schema.define(version: 20151203234708) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"
  enable_extension "pg_trgm"
  enable_extension "fuzzystrmatch"
  enable_extension "btree_gin"
  enable_extension "btree_gist"

if you do not see btree_gin enabled, you can not use the code

add_index :products, :data, using: :gin

you can install any module by running a migration like so. The changes will be reflected in your schema.rb

class InstallSomeContribPackages < ActiveRecord::Migration
  def up
    execute "CREATE EXTENSION IF NOT EXISTS btree_gin;"
    execute "CREATE EXTENSION IF NOT EXISTS btree_gist;"
  end

  def down
    execute "DROP EXTENSION IF EXISTS btree_gin;"
    execute "DROP EXTENSION IF EXISTS btree_gist;"
  end
end

here is a list of postgres modules

2 Comments

You can also add extensions in migrations with migration syntax stackoverflow.com/questions/16611226/… enable_extension "btree_gin"
And I would also note, that btree_gin is enough, at least for PostgreSQL 9.4

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.