This question already has answers here:
How do I handle too long index names in a Ruby on Rails ActiveRecord migration?
(6个答案)
去年关门了。
当我运行rails迁移命令时。我得到的索引名太长了。
我的迁移文件
服务器日志是
rails中止!
标准错误:发生错误,此迁移和所有以后的迁移都被取消:
索引名称“Index_mailboxer_conversation_opt_outs_on_unsubscriber_type_and_unsubscriber_id”表“mailboxer_conversation_opt_outs”太长;限制为63个字符
/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/connection_adapters/abstract/schema_语句
/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/connection_adapters/postgresql/schema_语句。rb:465:在create_table中的
/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/connection_adapters/abstract/schema_语句
/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/migration.rb:871:in
/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/migration.rb:840:in
/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/migration/compatibility.rb:36:in
/主页/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/migration.rb:604:in
/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/migration.rb:777:在“up”中
(6个答案)
去年关门了。
当我运行rails迁移命令时。我得到的索引名太长了。
我的迁移文件
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
服务器日志是
rails中止!
标准错误:发生错误,此迁移和所有以后的迁移都被取消:
索引名称“Index_mailboxer_conversation_opt_outs_on_unsubscriber_type_and_unsubscriber_id”表“mailboxer_conversation_opt_outs”太长;限制为63个字符
/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/connection_adapters/abstract/schema_语句
/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/connection_adapters/postgresql/schema_语句。rb:465:在create_table中的
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:in
块中/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/connection_adapters/abstract/schema_语句
/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/migration.rb:871: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:in
用时间阻塞say_/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/migration.rb:840: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:in
method_missing'/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/migration/compatibility.rb:36:in
block in method_missing' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/migration.rb:840:in
create_table'/主页/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/migration.rb:604:in
say_with_time' /home/sharat/.rvm/gems/ruby-2.4.3/gems/activerecord-5.2.0/lib/active_record/migration.rb:860:in
up'/home/sharat/.rvm/gems/ruby-2.4.3/gems/active record-5.2.0/lib/active_record/migration.rb:777:在“up”中
最佳答案
https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html
系统使用的标识符不超过NAMEDATALEN-1字节;
较长的名称可以在命令中写入,但它们将被截断。
默认情况下,NAMEDATALLN为64,因此最大标识符长度为63。
字节。如果这个限制有问题,可以通过更改
src/include/pg_config_manual.h中的NAMEDATALEN常量。
所以除非你真的想重新编译,否则63是标识符的硬限制
关于ruby-on-rails - 索引名称“index_name”太长;限制为63个字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50128995/
10-13 05:21