0

Hi I was trying to do migration rake db:migration to add new table as well as new indexes. However, I got this error:

Caused by:
ArgumentError: Index name 'index_cite_this_article_referencings_on_referencer_type_and_referencer_id' on table 'cite_this_article_referencings' is too long; the limit is 64 characters

Which is understandable, but after I define the name of index, the migration doesn't take my self-defined index name and still give the same error.

Here is my migration:

class CreateCiteThisArticleReferencings < ActiveRecord::Migration[5.2]
  def change
    create_table :cite_this_article_referencings do |t|
      t.belongs_to :article
      t.belongs_to :referencer, polymorphic: true

      t.timestamps
      t.index [:article_id], name: "cite_this_article_referencings_a_id", unique: true
      t.index [:referencer_id], name: "cite_this_article_referencings_r_id", unique: true
      t.index [:referencer_id, :referencer_type], name: "cite_this_article_referencings_all_ids", unique: true
    end

  end
end

The migration can't catch the cite_this_article_referencings_all_ids; instead, it still uses its own created name index_cite_this_article_referencings_on_referencer_type_and_referencer_id

Rails 5.2.3; Ruby 2.5.1; MacOS Mojave. Thanks for any help!

xc2333
  • 99
  • 3
  • 10
  • This has been answered before. https://stackoverflow.com/questions/5443740/how-do-i-handle-too-long-index-names-in-a-ruby-on-rails-activerecord-migration – Jason Brodie Dec 17 '19 at 19:45
  • @JasonBrodie Hi Thanks for the links. The `belong_to` is tricky... – xc2333 Dec 19 '19 at 17:14

1 Answers1

0

Provide the :name option to add_index, e.g.:

add_index :cite_this_article_referencings,
  [:referencer_id, :referencer_type], 
  :unique => true,
  :name => 'my_index'
code_aks
  • 1,972
  • 1
  • 12
  • 28