How do I create this specific PostGres index in a Rails migration?
Asked Answered
H

3

0

I’m using Rails 4.2.7 and PostGres 9.5. I’m trying to create a trqm index on my table (to facilitate LIKE searches) so I created this migration

class AddTrqmIndexOnRaceTimes < ActiveRecord::Migration
  def change
    CREATE INDEX my_object_times_name_gin_trgm_idx ON my_object_times USING gin (name gin_trgm_ops);
  end
end

This results in the below error when I run “rake db:migrate” …

StandardError: An error has occurred, this and all later migrations canceled:

undefined local variable or method `gin_trgm_ops' for #<AddTrqmIndexOnRaceTimes:0x007fdc34b40600>
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:664:in `block in method_missing'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:654:in `method_missing'
/Users/mikeb/Documents/workspace/runtrax/db/migrate/20161012185951_add_trqm_index_on_my_object_times.rb:3:in `change'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:608:in `exec_migration'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:591:in `block in migrate'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:590:in `migrate'

What’s the right way to create this index in a Rails migration?

Haith answered 12/10, 2016 at 19:4 Comment(0)
T
0

You cannot add SQL directly in the ruby method change. Either use the add_index method, or if you need to go beyond the capabilities of the helper, use ActiveRecord to execute your query like this:

class AddTrqmIndexOnRaceTimes < ActiveRecord::Migration
  def change
    MyObjectTime.connection.execute("CREATE INDEX my_object_times_name_gin_trgm_idx ON my_object_times USING gin (name gin_trgm_ops);")
  end
end
Toby answered 13/10, 2016 at 9:46 Comment(1)
How do I use the "add_index" method to execute the type of index I have specified in my question?Haith
C
0

Rails 5 adds support for specifying operator classes on expression indexes

for example:

def change
  add_index :patients, 'lower(last_name) varchar_pattern_ops', name: "index_patients_on_name_unique", unique: true
end

source

Carnay answered 22/1, 2021 at 14:45 Comment(0)
B
0

ou need to make slight changes to your code. Wrap the sql query in execute.

class AddTrqmIndexOnRaceTimes < ActiveRecord::Migration
  def change
    execute "CREATE INDEX my_object_times_name_gin_trgm_idx ON my_object_times USING gin (name gin_trgm_ops);"
  end
end
Busch answered 1/5, 2021 at 1:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.