I am on PostgreSQL 9.3.4
and Rails 4.0.4
.
I add a "tags" column, and corresponding gin
index (or, at least I ask for one).
class AddTagsToPhotos < ActiveRecord::Migration
def change
add_column :photos, :tags, :text, array: true, null: false, default: []
add_index :photos, :tags, using: 'gin'
end
end
Verify the results via psql
:
psql=# \d photos
...
tags | text[] | not null default '{}'::text[]
Indexes:
"index_photos_on_tags" btree (tags)
Notice that the "tags" index is of type btree
- whereas I asked for gin
.
Now manually create an index to show that gin
is available:
psql=# create index index_photos_on_tags2 on photos using gin(tags) ;
psql=# \d photos
Indexes:
"index_photos_on_tags" btree (tags)
"index_photos_on_tags2" gin (tags)
Indeed, gin
is available.
For the time being I am using this workaround with raw SQL, but would like to know why the typical approach above is failing:
class AddTagsToPhotos < ActiveRecord::Migration
def up
add_column :photos, :tags, :text, array: true, null: false, default: []
ActiveRecord::Base.connection.execute('create index index_photos_on_tags on photos using gin(tags) ;')
end
def down
ActiveRecord::Base.connection.execute('drop index index_photos_on_tags')
remove_column :photos, :tags
end
end
Note that there is another snag!
It turns out that db/schema.rb
will not have gin
set as the index type:
add_index "photos", ["tags"], :name => "index_photos_on_tags"
Potential interim workaround:
add_index "photos", ["tags"], :name => "index_photos_on_tags", using: :gin
Alert!
Until this bug is fixed, you must review changes to db/schema.rb
whenever you run a migration, as all future migrations will strip using: :gin
from the add_index
line.
execute
is defined in ActiveRecord::Migration, so you don't need to specifyActiveRecord::Base.connection
. – Apul