1

when I'm running rails migration command. I'm getting index name is too long. My migration file

class AddMissingIndices < ActiveRecord::Migration
  def change
    # We'll explicitly specify its name, as the auto-generated name is too long and exceeds 63
    # characters limitation.
    add_index :mailboxer_conversation_opt_outs, [:unsubscriber_id, :unsubscriber_type],
      name: 'index_mailboxer_conversation_opt_outs_on_unsubscriber_id_type'
    add_index :mailboxer_conversation_opt_outs, :conversation_id

    add_index :mailboxer_notifications, :type
    add_index :mailboxer_notifications, [:sender_id, :sender_type]

    # We'll explicitly specify its name, as the auto-generated name is too long and exceeds 63
    # characters limitation.
    add_index :mailboxer_notifications, [:notified_object_id, :notified_object_type],
      name: 'index_mailboxer_notifications_on_notified_object_id_and_type'

    add_index :mailboxer_receipts, [:receiver_id, :receiver_type]
  end
end

Server logs are

rails aborted!

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

Index name 'index_mailboxer_conversation_opt_outs_on_unsubscriber_type_and_unsubscriber_id' on table 'mailboxer_conversation_opt_outs' is too long; the limit is 63 characters /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:1353:in validate_index_length!' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:1166:inadd_index_options' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/connection_adapters/postgresql/schema_statements.rb:465:in add_index' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:315:inblock in create_table' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:314:in each' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:314:increate_table' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/migration.rb:871:in block in method_missing' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/migration.rb:840:inblock in say_with_time' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/migration.rb:840:in say_with_time' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/migration.rb:860:inmethod_missing' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/migration/compatibility.rb:36:in create_table' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/migration/compatibility.rb:75:increate_table' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/migration.rb:604:in method_missing' /home/sharat/rahul/Fleet-Latest/db/migrate/20170425092621_add_conversation_optout.mailboxer_engine.rb:4:inup' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/migration.rb:777:in `up'

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Rahul
  • 170
  • 1
  • 3
  • 14
  • 1
    This stackoverflow answer might help you. Check it out. https://stackoverflow.com/questions/5443740/how-do-i-handle-too-long-index-names-in-a-ruby-on-rails-migration-with-mysql –  May 02 '18 at 07:20
  • there's ahard limit for identfier (unless you compile different) – Vao Tsun May 02 '18 at 07:20
  • This stackoverflow answer might help you. Check it out.(https://stackoverflow.com/questions/5443740/how-do-i-handle-too-long-index-names-in-a-ruby-on-rails-migration-with-mysql) –  May 02 '18 at 07:21

2 Answers2

7

You can try creating the index with a custom (and shorter) name:

add_index(:accounts, [:branch_id, :party_id], unique: true, name: 'my_custom_and_shorter_name')

Since you already have the name field there, just change the index name :)

skozz
  • 2,662
  • 3
  • 26
  • 37
2

https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html

The system uses no more than NAMEDATALEN-1 bytes of an identifier; longer names can be written in commands, but they will be truncated. By default, NAMEDATALEN is 64 so the maximum identifier length is 63 bytes. If this limit is problematic, it can be raised by changing the NAMEDATALEN constant in src/include/pg_config_manual.h.

so unless you really want it and wish to recompile, 63 is a hard limit for the identifier

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
  • this type of error was not there for rails 5.0.7 when I updated my rails to 5.2 I am getting this error.... what I have to do to get rid of this error – Rahul May 02 '18 at 10:26