索引名称“index_name”太长;限制为63个字符[重复]

这个问题在这里已有答案:

当我运行rails migration命令时 . 我的索引名称太长了 . 我的迁移文件

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

服务器日志是

铁轨已中止! StandardError:发生错误,此以及所有后续迁移都已取消:表'mailboxer_conversation_opt_outs'上的索引名称'index_mailboxer_conversation_opt_outs_on_unsubscriber_type_and_unsubscriber_id'太长;限制是63个字符/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:create_table中的inblock'/home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record /connection_adapters/abstract/schema_statements.rb:314:每个'/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 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:在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'

回答(2)

2 years ago

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

系统使用的标识符不超过NAMEDATALEN-1个字节;较长的名称可以用命令编写,但它们会被截断 . 默认情况下,NAMEDATALEN为64,因此最大标识符长度为63个字节 . 如果此限制有问题,可以通过更改src / include / pg_config_manual.h中的NAMEDATALEN常量来引发此限制 .

所以除非你真的想要它并希望重新编译,否则63是标识符的硬限制

2 years ago

您可以尝试使用自定义(和更短)名称创建索引:

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

既然你已经有 name 字段,只需更改索引名称:)